Link Properties: Do NOT open PDF in browser

Jacob Phillips asked on January 24, 2017 15:33

As the title states I am trying to prevent a PDF from opening in a browser when a user clicks a download link on my site(Internet Explorer, Chrome, Firefox).

Currently I have a link on my page that is pointed to a PDF form. However, when a user clicks the link it opens the file in their browser.

Is there anyway to prevent this from happening?

I would prefer that the user is prompted to save the file to their local computer, and then open it via Adobe Reader or some other application that is compatible with PDFs (not their browser).

Is this possible?

Thank you. Jake

Correct Answer

Trevor Fayas answered on January 24, 2017 15:45

It is possible, it involves modifying the response to be an attachment instead of just showing.

Basically you will need to do this:

Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=thePDFFilename.pdf");
Response.TransmitFile(Server.MapPath("~/media/mysite/docs/ThePDFLocation.pdf"));
Response.End();

I would create a quick custom webpart that you can select the file path, set the file name, and set the mime type (the 3 values) and then have it execute that code.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on January 24, 2017 15:45 (last edited on January 24, 2017 16:52)

This is a setting in each of the browsers and computers viewing your site. So it can vary for each user. To get around this someone would have to specify what the default program is for opening a .pdf document. The other option would be to Google "force open in adobe reader" and see what other options there are.

** UPDATE **
I misunderstood your question, Trevors response will provide that file as a download as you are looking for. I was thinking you wanted it to open automatically in Reader which isn't very easy or possible from a the web side.

0 votesVote for this answer Mark as a Correct answer

Jacob Phillips answered on January 24, 2017 16:47

Trevor,

Thank you for your quick response. I have very basic web part development experience; however, I am very interested in trying your suggestion. Could you please advise me further on the code that is involved to reference the file path, set the file name, and set the mime type?

I am using Visual Web Developer 2010 Express to create the web part as a .ascx Web User Control.

Thank you. Jake

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on January 24, 2017 17:00

Change the documentation to whatever version of Kentico you are in:

https://docs.kentico.com/k10/custom-development/developing-web-parts/creating-new-web-parts

But what may be easier is just take an existing web part and clone it so you have a good 'starting' point, maybe something like the Static Text Web part, it shows the basic elements.

How kentico works when you add a webpart, you enter in the field values (Defined from Web Parts -> Your Web Part -> Properties tab). A webpart is a control (ascx), that it passes those values to so it renders.

So basically you'll make your web part with 3 properties (see the "public string Text" in the Static Text webpart), then in the SetupControl() (or the OnLoad) you can set your response logic.

1 votesVote for this answer Mark as a Correct answer

Jacob Phillips answered on January 24, 2017 21:48

Trevor,

Thank you for your continued input. As I stated I am not confident with my web part development skills; however, I am trying your suggestions. I will post my results here once I am able to. If you have any further suggestions please let me know.

Thank you. Jake

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on January 25, 2017 08:58

If you are generating these links through transformation or in any form on an HTML page you don't have to perform any rocket science feat. it's pretty simple. make use of HTML download attribute like this.

<a href="/files/adlafjlxjewfasd89asd8f.pdf" download="expenses.pdf">Download Your Expense Report</a>

Source - David Walsh blog. I have used this on my own website too. It works

1 votesVote for this answer Mark as a Correct answer

Jacob Phillips answered on January 26, 2017 16:39 (last edited on January 26, 2017 16:42)

Chetan, I did try your suggestion; however, by default each browser (Internet Explorer, Chrome, and Firefox), are all still opening the PDF within the browser. Chrome is the only browser that appears to recognize your suggestion. I would like to support all major browsers, and not just Chrome because I cannot predict what browser a user is utilizing.

In a normal circumstance this would not be an issue; however, the PDF is actually a form and some browsers do not support form features. So that is why I am trying to force the file to be downloaded and saved locally.

Thank you for your effort. Jake

0 votesVote for this answer Mark as a Correct answer

Jacob Phillips answered on January 26, 2017 18:34

Trevor,

Your suggestion works! I was able to create a basic Web Part by cloning the Simple Text web part, and modifying it per your responses.

Now when a user clicks the Download Form button, they are prompted to save or open the file. This is EXACTLY what I wanted it to do. I have tested this in all 3 major browsers (Internet Explorer, Chrome, Firefox), and it works in all three!

THANK YOU VERY MUCH! Jake

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on January 27, 2017 13:45 (last edited on January 27, 2017 13:45)

Thank you Jacob for updating me. Seems like the it doesn't support IE. However working fine for Edge, Mozilla and Chrome.

http://caniuse.com/#feat=download

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.