Friday, 2 October 2015

Force Download File in ASP.NET

Leave a Comment
This is example how to force current HTTP Context Response to download file.

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

In ASP.NET this is the way to set path. Since we can use tiddler("~") symbol to referring to the root with 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();  






By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.