Tip: File Download in ASP.NET and Tracking the Status of Success or Failure of Download


This article demonstrates how to provide download of a file in ASP.net along with tracking its success and failure. It will be useful especially in e-commerce system that offers downloadable product option. In e-commerce system it is very important to keep track of status of download. For a download option there can be two scenarios 1.complete/success download and 2.failure download.

In e-commerce system user may have a limited number of download allowed which is one in most of the cases. If a download is successful, it should update the record which will indicate the user that he has already downloaded the file or increment the download count by 1.  But for failure download the user should be able to download it again or the download count should remain same. So this article will help in tracking such status of download

While working on a e-commerce project I had a requirement to implement such a functionality where the success/failure of the download can be tracked. After searching for the solution I found that there is no such article related to similar problem. Then I came up with this solution after reading an article on transferring file in small packets. I hope this solution will help others struggling with similar problem.

Basics about download

When the function provided is called on click of download button, a similar window as shown below opens asking the user to Open, Save or Cancel

Clicking Open or Save will result in start of download where as Cancel will stop/fail the download.

A user can Cancel the Download even after Open or Save click, which need to be tracked.

The code involved

The code contains the basic logic of File Download in ASP.NET, which will not give end-user any hint of the location of the file. First we create System.IO.FileInfo object providing the complete file path, which will give us the file length. We also create a FileStream object which will be passed to BinaryReader object which in tern will help reading the data into bytes. Then we use the Response object to transfer the data.

//File Path and File Name
string filePath = Server.MapPath("~/ApplicationData/DownloadableProducts");
string _DownloadableProductFileName = "DownloadableProduct_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);

long startBytes = 0;
string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
string _EncodedData = HttpUtility.UrlEncode(_DownloadableProductFileName, Encoding.UTF8) + lastUpdateTiemStamp; 

//Clear the content of the response
Response.Buffer = false;
Response.AddHeader("Accept-Ranges", "bytes");
Response.AppendHeader("ETag", "\"" + _EncodedData +"\"");
Response.AppendHeader("Last-Modified", lastUpdateTiemStamp);

//Set the ContentType
Response.ContentType = "application/octet-stream";

//Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment;filename="+ FileName.Name);

//Add the file size into the response header
Response.AddHeader("Content-Length", (FileName.Length - startBytes).ToString());
Response.AddHeader("Connection", "Keep-Alive");

//Set the Content Encoding type
Response.ContentEncoding = Encoding.UTF8; 

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

There is no response coming back from the download window whether download is completed or aborted in between so it become more difficult to know the status of download. The Basic logic behind tracking the download status is transferring the file into smaller packets(size of packets can be kept as per convenience) and checking whether all the packets has been transferred. If there will be any failure in between the transfer of file total number of packets will be compared with number of packets transferred. This comparison will decide the status(Success/Failure) of download. For very small file it is difficult to track failure of download as the number of packets will be very few. So more effective tracking will happen if file size will be greater than 10kb.

In below code we are getting the total number of packets by dividing total bytes of data by 1024 to keep the packet size as 1kb. To send the data packets one by one we are using for loop. Using Response.BinaryWrite we are sending the 1024 bytes of data at a time which is read using BinaryReader.

//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++)
Then we compare the number of data packets transferred with total number of data packets which we 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 total number of data packets that indicates that there is some problem and transfer was not complete. 
//compare packets transferred with total number of packets
if (i < maxCount) return false;
return true;  

Close the Binary reader and File stream in final block. 

//Close Binary reader and File stream

About my company - Proteans Software Solutions

Proteans Software Solutions is an outsourcing company focusing on software product development and business application development on Microsoft Technology Platform. Committed to consistently deliver high-quality software products and services through continual improvement of our knowledge and practices focused on increased customer satisfaction.

About the Author

anil meharia

I have an experience of 2 years 8 months. I work for Proteans Software Solutions as software engg.



  • Nice article

    Posted by Fernando on 07/24/2014 02:54pm

    I've tested your code, but I would like to know if there is some way to display a confirmation message if the download was successful or not. Thanks in advance!

  • Als u op zoek bent naar goedkope koptelefoon, het aangaan van dr dre

    Posted by mrswanzi on 06/06/2013 04:55pm

    [url=http://koptelefoon-monsterbeats.tumblr.com/]Goedkope Dr Dre Beats [/url] De samenwerking met LeBron James past perfect in de endorsement marketing strategie die Monster Cable en Beats Electronics, het bedrijf van Dr. Dre en muziekproducent Jimmy Iovine, hebben uitgedacht. Nog voor de headphones voor de consument op de markt verschenen, liepen verschillende grote artiesten uit de Amerikaanse hip hop- en rapscene al met een Beats by Dr. Dre rond. [url=http://koptelefoon-monsterbeats.webspawner.com/]beats by dre[/url] Tijdens de afgelopen Olympische Spelen werd de populariteit van de Monster Beats by Dr. Dre hoofdtelefoons weer aangetoond. Zelfs een waarschuwing van het IOC, het Internationaal Olympisch Comit¨¦, kon niet voorkomen dat diverse sporters voor hun wedstrijden te zien waren met een Beats by Dr. Dre op het hoofd. Want hoe komen deze koptelefoons aan de ongekende populariteit De Beats by Dr. Dre hoofdtelefoons zijn hot. Heel erg hot. Loop buiten een rondje en de kans is meer dan groot dat je tijdens dat rondje minstens ¨¦¨¦n exemplaar tegen komt. Achter de opkomst van deze hype zit een goed uitgedachte strategie. Want hoe komen deze koptelefoons aan de ongekende populariteit [url=http://koptelefoon-monsterbeats.weebly.com/]beats by dre kopen[/url] Beats By Dre is domineert de hoofdtelefoon game al een tijdje, nu lijkt het alsof ze uit breidt naar de home audio markt met de introductie van de ¡®pil. ¡°De pil is een draagbare, draadloze luidspreker met een fantastisch geluid, net als de beats koptelefoon. Het beschikt over Bluetooth-mogelijkheden voor aansluitingen van maximaal 30 meter met een apparaat. Het beschikt ook over een interne microfoon die conference calls mogelijk maken.

  • Mu Liji Chloric is disconcerted affectedly sweet of supports Kelaiao to seize golden boots with all one's strength, chinese football, exceed in, football news, 7m sports news

    Posted by owdojcksxq on 05/17/2013 06:26pm

    Mu Liji: ? Chloric is disconcerted affectedly sweet? of supports [url=http://jordanscheap.page4.me/]air jordans cheap[/url] Kelaiao to seize golden boots with all one's strength, chinese football, exceed in, football news, 7m sports news Mu Liji: ? Chloric is disconcerted affectedly sweet? of supports Kelaiao to seize gold with all one's strength bootsWww.7m.cn On March 28, 2011 origin: Netease sports is in of new sports seasonLeague matches exceeds inIn, asKelaiao, AobinaWait for the affiliation of outstanding forward, make this sports season medium the competition of boots surpassing gold will more intense. AsChinaLeague matches once forward of the first day of priceMu Liji, also undertook expectation to the goal target of new sports season before camera lens. Basis " the football signs up for " message, mu Liji expresses self-confidently, oneself will advance more balls in new sports season, assist teammate to overcome Lai with all one's strength abstruse golden boots exceeds in capturing. Well-known, last year in July, orIn armourOf teamGuangzhou constant groupBought with the day price of 3.5 million dollarBrazilTalent forward Mu Liji. Although a lot of people are in at that time,this silver that oppugns Heng Dahua is worth after all not worth, but Muliji is used come on the stage 14, score a goal 13 report fight back doubt. And of these 13 goals containing Troy also is heft dye-in-the-wood, head show is right blastNanjing hasFinish big 4 happy event, and strong exceed battle to be opposite blastThe spring of dayThe free kick that finish is killed absolutely, the thing that proved Muliji exceeds a value. League matches exceeds in looking into the new sports season that is about to kick off, mu Liji expresses self-confidently, "Want to enter more ball " than last year. If go up according to him the goal efficiency of sports season, groups the Brazil of so new sports season is small can be at least in 30 matches infiltrate 20 goals. Do not spend new sports season, because constant group introduced wallop,stronger center overcomes Lai abstruse, advocate Shuai Lizhang Zhu made the Muliji with a constituent stronger capability be put in the tactics behind Kelaiao, and the important task core of the field in his appoint. Core of the field in regarding as, more tasks are series connection team attack defend a system, and the goal menace that this also means him is far not as advanced as the top sharp edge what Kelaiao comes to on-line is direct. But to such circumstance, mu Liji appears very insipid, he expresses, "Kelaiao is very good player, although cooperate time weak point now, but we will be tacitter and tacitter. But we will be tacitter and tacitter.. When the competition that exceeds golden boots in speak of, mu Liji expresses, "The level that exceeds in is sure level of the armour in comparing is high, this is the competition with a very intense competition, besides Guangzhou team, still haveShandongAndTianjinSuch powerful group, especially the Aobina of Shandong team, it is first-rate player, we are inAbility of the Luo Jing inside riceA partner passes when, aobina is the competitor " of optimal archer. And should ask about Aobina and Kelaiao who carries off when the odds of optimal archer is a few bigger, mu Liji expresses piquantly, "I feel Kelaiao's opportunity is a few greater, because his backside has me. Because his backside has me.. Be worth what carry is, after 3 Brazilians join in, " of fellow-townsman of 4 Brazil " lives in Pearl River new city together, discrepancy is together, and such life atmosphere also makes Muliji appears in the life of Guangzhou team more feel just like a fish in water. Believe such change, can make the Mu Liji expression in new sports season further upward.

  • Great job , and I've Q

    Posted by ab_dc on 06/24/2010 08:29am

    how to know the path where user downloads the document ? 
    also can we define a path to save document to ?

  • Detecting if the client is still connected

    Posted by lewismoten on 05/07/2010 01:41pm

    I would like to add that you should always check to see if the client is still connected as well.
    while (i < fileSize)
    if (!Response.IsClientConnected) return;
    // do logic to send next chunk
    Although you flagged the code to accept ranges, I noticed you never continue where the download stopped. Your code example always assumes the browser is starting with the first byte. Some browsers are able to recover from a failed download attempt and continue where they left off (given that the web server supports ranges).
    For content-disposition header, I recommend using the all settings - primarily the size and quoted file name. Quoted file name helps with files that have spaces in them, and the size helps with displaying the progress bar for lengthy downloads (Content-Length header isn't always enough for some browsers):
    attachment; filename="{0}"; size={1}; creation-date={2:R}; modification-date={3:R}; read-date={4:R}
    As a final note - be careful with your content type. Office 2007 documents with an application/octet stream will only open in WinZip on many computers using Internet Explorer 8 - even if you have the correct file extension. The browser will even rename the file extension to ".zip" as a security precaution. Make sure to include the correct content type when it is available.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date