Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force browser to download PDF document instead of opening it

Tags:

I want to make the browser download a PDF document from server instead of opening the file in browser itself. I am using C#.

Below is my sample code which I used. It not working..

string filename = "Sample server url"; response.redirect(filename); 
like image 225
Arun Avatar asked Dec 21 '11 13:12

Arun


People also ask

How do I download a PDF forcefully instead of opening it in a browser?

Click “Site Settings” on the right. Scroll down in Site Settings and click “Additional content settings” at the very bottom. In the expanded menu, select “PDF documents.” Toggle on the “Download PDF files instead of automatically opening them in Chrome” option.

How do I get Chrome to download instead of PDF?

In Google Chrome go to Settings. Scroll to the bottom of the page and click on the hyperlink for Show Advanced Settings. From there scroll down until you find the "Download" section. Uncheck the box where it asks where to save each file before downloading.


1 Answers

You should look at the "Content-Disposition" header; for example setting "Content-Disposition" to "attachment; filename=foo.pdf" will prompt the user (typically) with a "Save as: foo.pdf" dialog, rather than opening it. This, however, needs to come from the request that is doing the download, so you can't do this during a redirect. However, ASP.NET offers Response.TransmitFile for this purpose. For example (assuming you aren't using MVC, which has other preferred options):

Response.Clear(); Response.ContentType = "application/pdf"; Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf"); Response.TransmitFile(filePath); Response.End();  
like image 69
Marc Gravell Avatar answered Oct 13 '22 14:10

Marc Gravell