Thursday, June 10, 2010

Download a file with Save as dialog in ASP.NET

For the file in your web server folder. you can use below code to download with save as dialog in ASP.NET



Response.Clear();
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition","attachment; filename=photo.jpg");
Response.TransmitFile( Server.MapPath("~/images/photo.jpg") );
Response.End();

If you want to download a file with save as dialog from dynamically generated data, see below example


Response.Clear();
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=filterlist.csv");
string outputtext="dynamic data";
Response.Write(outputtext);
Response.End();

No comments:

Post a Comment