Friday, February 6, 2009

File download in ASP.net

Everybody downloads files from internet, every time you click download button browser shows a “Save As” dialog box from which you can choose the location to save the file. The same can be implemented in our asp.net website by using the folowing code snippet :

string FilePath = Server.MapPath("Filename");//Gets the physical path to the file.
Response.ContentType = "Application/octet-stream";//This will say it’s a downloadable file
Response.AppendHeader("content-disposition","attachment; filename=" + "Filename"); //To show force download “Save As”

//Now write the file directly to the HTTP content output stream.

Response.WriteFile(FilePath);
Response.End();

Note: Response.AppendHeader method is used to add the "content-disposition" which forces the File Download (save) dialog.

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails