Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET file download from server

Tags:

c#

asp.net

After a user clicks a button, I want a file to be downloaded. I've tried the following which seems to work, but not without throwing an exception (ThreadAbort) which is not acceptable.

    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;     response.ClearContent();     response.Clear();     response.ContentType = "text/plain";     response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");     response.TransmitFile(Server.MapPath("FileDownload.csv"));     response.Flush();     response.End();   
like image 521
James Stevenson Avatar asked Aug 28 '13 00:08

James Stevenson


People also ask

How do you make a file downloadable in C#?

The simply way how to download file is to use WebClient class and its method DownloadFile. This method has two parameters, first is the url of the file you want to download and the second parameter is path to local disk to which you want to save the file.

How can we upload and download the file from the server?

Uploading or downloading files:Click the right mouse button on file(s) on the left side of client and select Upload. Your file(s) will be uploaded to the server. Click the right mouse button on file(s) on the right side of client and select Download. Your file(s) will be downloaded to your computer.


1 Answers

You can use an HTTP Handler (.ashx) to download a file, like this:

DownloadFile.ashx:

public class DownloadFile : IHttpHandler  {     public void ProcessRequest(HttpContext context)     {            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;         response.ClearContent();         response.Clear();         response.ContentType = "text/plain";         response.AddHeader("Content-Disposition",                             "attachment; filename=" + fileName + ";");         response.TransmitFile(Server.MapPath("FileDownload.csv"));         response.Flush();             response.End();     }      public bool IsReusable     {         get         {             return false;         }     } } 

Then you can call the HTTP Handler from the button click event handler, like this:

Markup:

<asp:Button ID="btnDownload" runat="server" Text="Download File"              OnClick="btnDownload_Click"/> 

Code-Behind:

protected void btnDownload_Click(object sender, EventArgs e) {     Response.Redirect("PathToHttpHandler/DownloadFile.ashx"); } 

Passing a parameter to the HTTP Handler:

You can simply append a query string variable to the Response.Redirect(), like this:

Response.Redirect("PathToHttpHandler/DownloadFile.ashx?yourVariable=yourValue"); 

Then in the actual handler code you can use the Request object in the HttpContext to grab the query string variable value, like this:

System.Web.HttpRequest request = System.Web.HttpContext.Current.Request; string yourVariableValue = request.QueryString["yourVariable"];  // Use the yourVariableValue here 

Note - it is common to pass a filename as a query string parameter to suggest to the user what the file actually is, in which case they can override that name value with Save As...

like image 163
Karl Anderson Avatar answered Oct 11 '22 19:10

Karl Anderson