Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to download a PowerPoint file using C#

0.00/5 (No votes)
5 Sep 2012 1  
Recently I had to write few lines of code to download ppt files from asp.net.

Introduction

Recently I had to write a few lines of code to download a PowerPoint presentation file from one of my ASP.NET websites. I would like to share the block of code I used for this purpose.

if (File.Exists(Server.MapPath(file)))
{			
   Response.ClearHeaders();
   Response.Clear();			
   Response.ContentType = "application/x-mspowerpoint";
   Response.AddHeader("Content-Disposition", "attachment; filename=" + file);
   Response.WriteFile(Server.MapPath(file));
   Response.Flush();
}

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 MS Word you should set application/msword, for a 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 the Page_load event. So, for this you may create a page Download.aspx and redirect to that page when you click the Download button with file information in the querystring. 

Hope it helps.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here