Click here to Skip to main content
15,881,812 members
Articles / Web Development / ASP.NET
Tip/Trick

How to download powerpoint file using C#

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
8 Mar 2012CPOL 37.1K   3  
Recently I had to write few lines of code to download ppt files from asp.net.
This is an old version of the currently published tip/trick.

Introduction

Recently I had to write few lines of code to download power point presentation file from one of my asp.net website. 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 msword you should set application/msword, for gif image you should set image/gif, for pdf you should set <span style="font-family: 'Segoe UI', Verdana, Arial; line-height: 18px; text-align: left; ">application/pdf</span><span style="line-height: 18px; text-align: left; "> </span><span style="color: rgb(0, 0, 0); font-family: Verdana, Arial, sans-serif; font-size: 13px; ">etc.</span> 

Here is the reference to set Response.ContentType :

http://msdn.microsoft.com/en-us/library/ms775147.aspx 

http://forums.asp.net/t/1159236.aspx 

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.  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
Software Engineer | Software Architect | System Designer | System Analyst | Team Leader | Consultant (.Net)

12 Years of Experience in the Industry.

Currently working as System Designer at CIMSOLUTIONS, Netherlands


View My Profile in LinkedIn

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.