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;
- }
- }
-
- }
Inside the process request part of the generated code, you will need to delete its current contents, i.e. these, lines:context.Response.ContentType = "text/plain"; context.Response.Write("Hello World")
;
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));
-
-
- context.Response.End();
-
- }
- else
- {
- context.Response.ContentType = "text/plain";
- context.Response.Write("File not be found!");
-
-
- }
What does this code do? Well, I’m glad you asked. First, it creates a string from the query string value, which is the file to be downloaded by the user. Next, it checks to make sure the newly created string is not empty and the file exists. If it passes these checks, the code can move on but if the checks have not been passed, the code takes you to the else block, which informs the user that the file cannot 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>
You can see your link in the download.ashx file with a query string that informs the code which file the user wishes to download. In this case, it is an image file called cat1.jpg, which resides in a folder called the images. It is easy to implement and the code can be reused. Wonderful isn’t it? Now, you can start offering some goodies to your website users.
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.