ASP.NET/C# Prompt a Save Dialog Box to Download a File
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
Thank you soooo much! This worked for me.
man this works for one imagebutton but didnot work for the second imagebutton on the same page.Will you please give a good reason for this
This works perfectly and solves a lot of problems – thank you.
I am getting an exception in the last line of code Unauthorized exception was unhand led by users code