beta.blog

C# – Start a File Download in ASP.NET

by on Sep.11, 2013, under Programming

The following Microsoft ASP.NET example code will transmit a file to the client. Please be aware that the function GetMimeMapping requires .NET 4.5 or higher.

System.IO.FileInfo fi = new System.IO.FileInfo("hello.txt");
string name = fi.Name;
string length = fi.Length;

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fi.Name);
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = System.Web.MimeMapping.GetMimeMapping(fi.FullName); // .NET 4.5+ (!!)
Response.Flush();
Response.TransmitFile(fi.FullName);
Response.End();

1 Comment for this entry

  • khaled

    All over the world there are NO cracks for “iolo system mechanic pro v12.0” and we wish that you can help us with an activator for this version because the old activators couldn’t activate this version.thank you very very very much “betamaster”(mastermind).

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!