Programming
C# – Start a File Download in ASP.NET
by admin 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();