Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to refresh the page or Update the UI after downloading the file( tracking the file download)?

$
0
0

I am trying to download a file which needs to track whether file downloaded succesfully or not.

Based on the result i need to update the UI with succssfuly or unsuccesfuly download message.

Could someone help me on it.

Below is the code which i am using

protected void button_Click(object sender, EventArgs e) {
        bool isDownload = false;
        string url = "this will be the file URL";
        isDownload = StreamNew(url);
        if (isDownload)
        {
            // Response.AddHeader("Refresh", "3; url=Stream.aspx?s=t");
            lblMsg.Text = "Download Success";
            btn.Visible = false;

        }
        else
        {
            lblMsg.Text = "Download Failed";
        }
    }

    private bool StreamNew(string filePath)
    {
        //This controls how many bytes to read at a time and send to the client
        int bytesToRead = 10;

        // Buffer to read bytes in chunk size specified above
        byte[] buffer = new Byte[bytesToRead];

        // The number of bytes read
        try
        {

            //Create a WebRequest to get the file
            HttpWebRequest fileReq = (HttpWebRequest)WebRequest.Create(filePath);

            //Create a response for this request
            using (HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse())
            {
                if (fileReq.ContentLength > 0)
                    fileResp.ContentLength = fileReq.ContentLength;

                //Get the Stream returned from the response
                using (System.IO.Stream stream = fileResp.GetResponseStream())
                {

                    // prepare the response to the client. resp is the client Response
                    var httpResponse = HttpContext.Current.Response;
                    //Clear the content of the response
                    httpResponse.Clear();
                    httpResponse.Buffer = false;
                    httpResponse.AddHeader("Accept-Ranges", "bytes");
                    //Indicate the type of data being sent
                    httpResponse.ContentType = "application/octet-stream";

                    //Add the file name and attachment,
                    //which will force the open/cancel/save dialog to show, to the header
                    httpResponse.AddHeader("Content-Disposition","attachment; filename=" + "ABC.zip");

                    //Add the file size into the response header
                    httpResponse.AddHeader("Content-Length", fileResp.ContentLength.ToString());
                    //Set the Content Encoding type
                    httpResponse.ContentEncoding = Encoding.UTF8;
                    int length;
                    do
                    {
                        // Verify that the client is connected.
                        if (httpResponse.IsClientConnected)
                        {
                            // Read data into the buffer.
                            length = stream.Read(buffer, 0, bytesToRead);

                            // and write it out to the response's output stream
                            httpResponse.OutputStream.Write(buffer, 0, length);

                            // Flush the data
                            httpResponse.Flush();

                            //Clear the buffer
                            buffer = new Byte[bytesToRead];
                        }
                        else
                        {
                            // cancel the download if client has disconnected
                            length = -1;
                        }
                    } while (length > 0); //Repeat until no data is read

                    return httpResponse.IsClientConnected;
                }
            }
        }
        catch (Exception ex)
        {

        }
        return false;

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>