ASP.NET/C# Prompt a Save Dialog Box to Download a File

May 15th, 2009
By: Cory Mathews

To save you all (as well as my future self) the trouble of searching all over the place just to find terrible answers on almost every form post out there. Here is a small ASP.NET/C# code snippet that will prompt the user with the save/open dialog box to download a file.

String FileName = "FileName.txt";
String FilePath = "C:/...."; //Replace this
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(FilePath);
response.Flush();
response.End();

For more content types check our http://en.wikipedia.org/wiki/MIME_type#List_of_common_media_types