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

How to download a PowerPoint file using C#

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
5 Sep 2012CPOL 37K   3   6
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.

C#
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, 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

 
QuestionThank Pin
gufran osource25-Jul-12 20:53
gufran osource25-Jul-12 20:53 
QuestionWhy is content type application/x-unknown used? Pin
pietvredeveld9-Mar-12 1:39
pietvredeveld9-Mar-12 1:39 
AnswerRe: Why is content type application/x-unknown used? Pin
Mahmud Hasan9-Mar-12 2:32
Mahmud Hasan9-Mar-12 2:32 
AnswerRe: Why is content type application/x-unknown used? Pin
Mahmud Hasan9-Mar-12 4:45
Mahmud Hasan9-Mar-12 4:45 
Suggestionplease update code example Pin
pietvredeveld9-Mar-12 1:37
pietvredeveld9-Mar-12 1:37 
AnswerRe: please update code example Pin
Mahmud Hasan9-Mar-12 2:33
Mahmud Hasan9-Mar-12 2:33 
I had put a new version of this write-up. Seems that is not yet available here. Codeproject is really having some problem in the new approval process.
Mahmud Hasan
Software Engineer from Bangladesh

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.