In a previous article, I discussed with you how to allow users to upload files to the Server via ASP:fileupload control. You can see it here.
In this article, I will be discussing with you how to download the files from the Server. We will be creating a generic handler to do all the heavy lifting. In this way, the code can be re-used, whenever and wherever it is required.
As usual, I recommend creating a new Website to play around with. Hence, open up Visual Studio and create a new empty ASP.NET Website (File > new > website) or (Shift + Alt + N).
In your shiny new Website, we need to add a generic handler, so go to (website > add new item) or (Ctrl + Shift + A).
Select generic handler from the list. I called mine download.ashx.
Visual Studio will do some of the hard work for you by generating the following:
- <%@ WebHandler Language="C#" Class="Download" %>
- using System;
- using System.Web;
- public class Download : IHttpHandler {
- public void ProcessRequest (HttpContext context) {
- context.Response.ContentType = "text/plain";
- context.Response.Write("Hello World");
- }
- public bool IsReusable {
- get {
- return false;
- }
- }
- }
Once, you have deleted those lines, add the following code:
- string file = context.Request.QueryString["file"];
- if (!string.IsNullOrEmpty(file) && File.Exists(context.Server.MapPath(file)))
- {
- context.Response.Clear();
- context.Response.ContentType = "application/octet-stream";
- context.Response.AddHeader("content-disposition", "attachment;filename=" + Path.GetFileName(file));
- context.Response.WriteFile(context.Server.MapPath(file));
- // This would be the ideal spot to collect some download statistics and / or tracking
- // also, you could implement other requests, such as delete the file after download
- context.Response.End();
- }
- else
- {
- context.Response.ContentType = "text/plain";
- context.Response.Write("File not be found!");
- }
Now, the interesting stuff happens. First, we clear the output before. We fill it in case there is any data lurking around in there that we’ve forgotten about. Next, we set the content type to ‘application/octet-stream’ which is a binary file. In this way, all the file types are covered. Subsequently, we move onto the next line, ‘AddHeader(("content-disposition", "attachment;filename=" + Path.GetFileName(file))’. This lets the user save the file on his computer and then decide how to use it, instead of the Browser trying to use the file. Next, we write the file and call Response.End(), which sends all currently buffered output to the client, stops execution of the page and raises the System.Web.HttpApplication.EndRequest event.
To use the generic handler we have just created; you will need to add a link into your Webform, as shown below:
- <a href="Download.ashx?file=images/cat1.jpg">cat pic</a>
I hope you found this article helpful and informative. If you have any comments, questions, queries etc. then please feel free to comment below or get in touch via email at [email protected]
I look forward to hearing your thoughts.

Anders NielsenPosted Jan 25, 2021, 10:08 AM
Hi Michael - are you not missing System.IO namespace? BR /Anders
Dhivakhar VenkatachalamPosted Sep 6, 2018, 9:37 AM
Good explanation....but i have a situation where the file is being automatically downoaded after the process is complete, now if i try to use response.redirect to download.asnx it will go to another page and so here can you please suggest how i can retain the response of the same page? Thanks in advance.
Munesh SharmaPosted Jul 3, 2016, 1:06 PM
Nice
Humza TufailPosted Jul 1, 2016, 3:10 AM
Nice one
Vignesh ManiPosted Jun 30, 2016, 5:39 PM
Good one
Michael GriffithsPosted Jun 30, 2016, 2:11 AM
Thank you
kalu singh raoPosted Jun 30, 2016, 1:31 AM
Nice...
RajaPosted Jun 30, 2016, 12:23 AM
Nice Share...
Santhakumar MunuswamyPosted Jun 30, 2016, 12:06 AM
Thank you for nice article