beta.blog

Archive for September, 2013

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 more...

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!