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.
Force download method :
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();