Rashim's Blog

Download Files in Asp.net

Posted on: June 4, 2013

Sometimes client applications need to retrieve different types of file (.doc, .xls, .zip…etc.) from web application to do some of their internal works or even just for the users. So, if we could make such web application which provides the facility to the client applications to download different files from the server, we don’t need to change or modify the code of the web application each time we upload the different types of file to the server.

This can be easily achieved by using an HTTP Handler for download operation. We can use normal asp.net web page for this purposes, but the reason behind to choose an HTTP Handler is only for performance issues. Firstly, we get benefit of scalability as we don’t need to show any output value to the page. Secondly, an HTTP handler doesn’t go through the full page events which ultimately improve performance.

The handler code has given below which is self-descriptive and requires no more description,

public class Download : IHttpHandler
 public bool IsReusable
     return false;

 public void ProcessRequest(HttpContext context)
    var request = context.Request;
    Stream stream = null;
    var buffer = new byte[2048];
    var fileName = request["FileName"];

    if (string.IsNullOrEmpty(fileName))
       throw new Exception("File name is Empty");

    var fullFileName = Path.Combine(string.Format("{0}", request.PhysicalApplicationPath), "DownloadFiles",fileName);
       stream = new FileStream(fullFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
       var dataToRead = stream.Length;
       while (dataToRead > 0)
          if (context.Response.IsClientConnected)
             var length = stream.Read(buffer, 0, 2048);
             context.Response.OutputStream.Write(buffer, 0, length);
             buffer = new byte[2048];
             dataToRead = dataToRead - length;
             dataToRead = -1;
    catch (Exception ex)
       if (stream != null)

The client applications need to send a request to the download handler appending the Filename variable in the QueryString. This is also a laid-back substance. I am giving a sample code for that,

 var fileName = string.Format("{0}","Install.zip");
 var queryString = string.Format("{0}{1}{2}", "FileName", "=", fileName);
 var downloadUrl = URL of Download.ashx;
 var url = string.Format("{0}{1}{2}", downloadUrl,"?", queryString);
 var downBuffer = new byte[2048];

 var webRequest = (HttpWebRequest)WebRequest.Create(url);
 webRequest.KeepAlive = false;
 webRequest.UnsafeAuthenticatedConnectionSharing = true;
 webRequest.Credentials = CredentialCache.DefaultCredentials;
 var webResponse = (HttpWebResponse)webRequest.GetResponse();
 var streamResponse = webResponse.GetResponseStream();
 var fileStream = new FileStream(string.Format("{0}{1}", "D:\\", fileName), FileMode.Create, FileAccess.Write);

 if (streamResponse != null)
     int bytesRead;
     while ((bytesRead = streamResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
         fileStream.Write(downBuffer, 0, bytesRead);


Here, I host the web application to the local IIS and the URL for download handler is ‘…/Download.ashx’. WebRequest is for making a request to a URI and HttpWebRequest offers an HTTP specific operation of WebRequest where HttpWebResponse delivers a response from a URI.

Download SourceCode

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Blog Stats

  • 189,442 hits


%d bloggers like this: