Force to download file in ASP.NET

This is an example of how to force the current HTTP Context Response to download files.

Before that, you need to set a path/folder in your web application to store files that you just created or pre-set the file to download.

In ASP.NET this is the way to set a path. Since we can use the tiddler("~") symbol to referring to the root with a virtual path set from IIS.

  string pathFile = Server.MapPath(Request.ApplicationPath);  


Force download method :

   
string pathFile = Server.MapPath(Request.ApplicationPath);
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Transfer-Encoding", "Binary");
Response.AddHeader("Content-disposition", "attachment; filename=\"" + yourfilename + ".csv\"");
Response.WriteFile(pathFile + yourfilename + ".csv");
Response.End();


Popular posts from this blog

How to create zip file to download in JSP- Servlet

How to create DataGrid or GridView in JSP - Servlet

Pinging in ASP.NET Example