Sunday, March 11, 2012

How to download powerpoint file using C#/


Recently I had to write few lines of code to download power point presentation file from one of my website. I would like to share the block of code I used for this purpose.

if (File.Exists(Server.MapPath(file)))
   Response.ContentType = "application/x-mspowerpoint";
   Response.AddHeader("Content-Disposition", "attachment; filename=" + file);
Here Response.ContentType should be set correctly. I have used  "application/x-mspowerpoint" for powerpoint. You need to set the correct ContentType. For example, for msword you should set application/msword, for gif image you should set image/gif, for pdf you should set application/pdf etc. 
Here is the reference to set Response.ContentType :

Another important information: 

To have it work correctly in IE you need to write the code in Page_load event. So, for this you may create a page - Download.aspx and redirect to that page when you click download button with file information in the querystring.

Hope it helps.  

No comments:

Post a Comment