Click here to Skip to main content
Click here to Skip to main content

How to download a PowerPoint file using C#

By , 5 Sep 2012
 

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mahmud Hasan
Software Developer (Senior) The Jaxara IT Ltd
Bangladesh Bangladesh
Member
An Expert Analyst | Architect | Developer | Consultant in ASP.NET and SQL SERVER based systems development with 7 years of experience in the industry.
 
Currently working as lead software engineer at THE JAXARA IT Ltd, one of the deserving offshore software companies in Bangladesh.
 
Connect with me over LinkedIn

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThank Pinmembergufran osource25 Jul '12 - 20:53 
QuestionWhy is content type application/x-unknown used? Pinmemberpietvredeveld9 Mar '12 - 1:39 
Suggestionplease update code example Pinmemberpietvredeveld9 Mar '12 - 1:37 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 5 Sep 2012
Article Copyright 2012 by Mahmud Hasan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid