Tracking the status of Download files in ASP.NET

Function for File Download in ASP.Net in C# and

Tracking the status of success/failure of Download.
private bool DownloadableProduct_Tracking()
    //File Path and File Name
    string filePath = Server.MapPath("~/ApplicationData/DownloadableProducts");
    string _DownloadableProductFileName = "Test_FileName.pdf";

    System.IO.FileInfo FileName = new System.IO.FileInfo(filePath + "\\" + _DownloadableProductFileName);
    FileStream myFile = new FileStream(filePath + "\\" + _DownloadableProductFileName, FileMode.Open, FileAccess.Read,       FileShare.ReadWrite);

    //Reads file as binary values
    BinaryReader _BinaryReader = new BinaryReader(myFile);

    //Check whether file exists in specified location
    if (FileName.Exists)
        long startBytes = 0;
        string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
        string _EncodedData = HttpUtility.UrlEncode(_DownloadableProductFileName, Encoding.UTF8) + lastUpdateTiemStamp;

        Response.Buffer = false;
        Response.AddHeader("Accept-Ranges", "bytes");
        Response.AppendHeader("ETag", "\"" + _EncodedData + "\"");
        Response.AppendHeader("Last-Modified", lastUpdateTiemStamp);
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName.Name);
        Response.AddHeader("Content-Length", (FileName.Length – startBytes).ToString());
        Response.AddHeader("Connection", "Keep-Alive");
        Response.ContentEncoding = Encoding.UTF8;

        //Send data
        _BinaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);

        //Dividing the data in 1024 bytes package
        int maxCount = (int)Math.Ceiling((FileName.Length – startBytes + 0.0) / 1024);

        //Download in block of 1024 bytes
        int i;
        for (i = 0; i < maxCount && Response.IsClientConnected; i++)
        //if blocks transfered not equals total number of blocks
        if (i < maxCount)
            return false;
        return true;
        return false;
    else System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(),
        "FileNotFoundWarning","alert(‘File is not available now!’)", true);
    return false;
NOTE: I am getting the total number of packets by dividing the total bytes
of data by 1024 to keep the packet size as 1 KB. To send the data
packets one by one, I am using
for loop. Using Response.BinaryWrite,
I am sending the 1024 bytes of data at a time which is read using

Then I compare the number of data packets transferred with the total
number of data packets which I calculated by dividing file length by
1024. If both the parameters are equal, that means that file transfer is
successful and all the packets got transferred. If number of data
packets transferred are less than the total number of data packets, that
indicates there is some problem and the transfer was not complete.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s