Force Browser to Download PDF Document Instead of Opening It

(HTML) Download a PDF file instead of opening them in browser when clicked

There is now the HTML 5 download attribute that can handle this.

I agree, and think Sarim's answer is good (it probably should be the chosen answer if the OP ever returns). However, this answer is still the reliable way to handle it (as Yiğit Yener's answer points out and--oddly--people agree with). While the download attribute has gained support, it's still spotty:

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

Force browser to download PDF document instead of opening it

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();

Can I force the browser to download the PDF file instead of opening it?

If you want the browser to download as attachment, you need to say so using the Content-Disposition header field. See http://greenbytes.de/tech/webdav/rfc6266.html#disposition.type, disposition type "attachment".

Open PDF in browser instead of downloading it

From what you're experiencing, it seems to me that Composite have gotten the MIME type of your uploaded file wrong, and is therefor not correctly telling the browser that this file is a pdf, and the browser doesn't know what to do with it.

  1. Try deleting the file and uploading it again.
  2. Try add ?download=false and the end of the href to the file. You prob. need to go into source mode of the content editor.

This is the exact line in the Source Code which is responsible for this behavior, and the logic is as follows

  • If there is no Querystring named download, the attachment is determined by the Mime Type. Only png, gif and jpeg will be shown inline, the rest will be shown as attachment.
  • If there is a Querystring named download with a value of false, it will override the Mime Type check and always force the Content-Disposition to be inline.

I made a quick test here to show that the behaviour is a expected. At least in my Chrome browser in Windows 8

  • Force download: https://www.dokument24.dk/media/9fdd29da-dde8-41f7-ba4c-1117059fdf06/z8srMQ/test/Prisblad%202015%20inkl%20moms.pdf
  • Show in browser: https://www.dokument24.dk/media/9fdd29da-dde8-41f7-ba4c-1117059fdf06/z8srMQ/test/Prisblad%202015%20inkl%20moms.pdf?download=false


Related Topics



Leave a reply



Submit