Rectangle 27 0

http headers How to force files to open in browser instead of download (pdf)?


Content-Type: application/pdf
Content-Disposition: attachment; filename="filename.pdf"

@ColinM Thanks buddy, you said it right, the issue when we debugged was the mime type was set while uploading the files. This should be done by the back-end team. I tried to get codes on how to add headers in java script but was not successful. Thanks, as I got the real idea cleared from you... :)

@Kailas I don't understand what you're trying to do.. The answer is referring to the headers that a server should send to a client when responding to an HTTP request for the PDF file. These headers have no effect on a file upload, you need to have the code behind the url set the headers every time it is downloaded by the client.

@PapickG.Taboada but then the user's system may not know the file type. E.g. some user's may have opted to "Always open files of this type" for PDF files. Perhaps if you want to override the user's preferences then octet-stream would be the way to go, but giving the correct type and a suggested filename is the "correct" way to provide a download.

EDIT: The quotes around the filename are required if the filename contains special characters such as filename[1].pdf which may otherwise break the browser's ability to handle the response.

To have the file downloaded rather than viewed:

To indicate to the browser that the file should be viewed in the browser:

hi @ColinM I am a bit confused here... we are having issues rendering the pdf, it just gives a scrambled text. where do we set the Content-Type: application/pdf Content-Disposition: inline; "filename.pdf"? 'cos, we upload it using angular-js code. So my question is should the content type be set before uploading? And also, we get only a link from the backend team, a url that gives the file path, that we open in new tab using:window.open(url, '_blank').focus();

Note
Rectangle 27 0

http headers How to force files to open in browser instead of download (pdf)?


Content-Type: application/pdf
Content-Disposition: attachment; filename="filename.pdf"

@ColinM Thanks buddy, you said it right, the issue when we debugged was the mime type was set while uploading the files. This should be done by the back-end team. I tried to get codes on how to add headers in java script but was not successful. Thanks, as I got the real idea cleared from you... :)

@Kailas I don't understand what you're trying to do.. The answer is referring to the headers that a server should send to a client when responding to an HTTP request for the PDF file. These headers have no effect on a file upload, you need to have the code behind the url set the headers every time it is downloaded by the client.

@PapickG.Taboada but then the user's system may not know the file type. E.g. some user's may have opted to "Always open files of this type" for PDF files. Perhaps if you want to override the user's preferences then octet-stream would be the way to go, but giving the correct type and a suggested filename is the "correct" way to provide a download.

EDIT: The quotes around the filename are required if the filename contains special characters such as filename[1].pdf which may otherwise break the browser's ability to handle the response.

To have the file downloaded rather than viewed:

To indicate to the browser that the file should be viewed in the browser:

hi @ColinM I am a bit confused here... we are having issues rendering the pdf, it just gives a scrambled text. where do we set the Content-Type: application/pdf Content-Disposition: inline; "filename.pdf"? 'cos, we upload it using angular-js code. So my question is should the content type be set before uploading? And also, we get only a link from the backend team, a url that gives the file path, that we open in new tab using:window.open(url, '_blank').focus();

Note
Rectangle 27 0

http headers How to force files to open in browser instead of download (pdf)?


<a href="somepathto.pdf" download="filename">

If you are using HTML5 (and i guess now a days everyone uses that), there is an attribute called download.

If you have control over the server code you should use 'attachement' as this will allow to use the same filename generation code. If you have no control over the server this is a good solution.

Important to note that this does not work across domains (e.g., same-origin policy gets in the way). If downloading from one domain, the download attribute will not work if content is stored on a different domain. CORS may allow that content to pass through (haven't tested).

This is a brilliant solution to the problem however, as always, IE is holding us back from using it: caniuse.com/#search=download

This is literally the opposite of what the OP is asking :)

here filename is optional, but if provided, it will take this name for downloaded file.

Note
Rectangle 27 0

http headers How to force files to open in browser instead of download (pdf)?


header("Content-Type: application/force-download");
    header("Content-type: application/pdf");
    header("Content-Disposition: inline; filename=\"".$name."\";");

If you don't want the browser to prompt the user then use "inline" for the third string instead of "attachment". Inline works very well. The PDF display immediately without asking the user to click on Open. I've used "attachment" and this will prompt the user for Open, Save. I've tried to change the browser setting nut it doesn't prevent the prompt.

Note
Rectangle 27 0

http headers How to force files to open in browser instead of download (pdf)?


header("Content-Disposition", "inline; filename=myfilename.myextension");
header("Content-Disposition", "attachment; filename=myfilename.myextension");
  • Use the HTML5 download attribute, uses custom filename specified on the view side.

If you have control over the server code:

NOTE: I prefer setting the filename on the server side as you may have more information and can use common code.

No control over the server code:

The correct type is application/pdf for PDF, not application/force-download, this looks like a hack for some legacy browsers. Always use the correct mimetype if you can.

Note
Rectangle 27 0

http headers How to force files to open in browser instead of download (pdf)?


<FilesMatch "\.(?i:pdf)$">
    ForceType application/octet-stream
    Header set Content-Disposition attachment
</FilesMatch>
Note
Rectangle 27 0

http headers How to force files to open in browser instead of download (pdf)?


<a href="somepathto.pdf" download="filename">

If you are using HTML5 (and i guess now a days everyone uses that), there is an attribute called download.

If you have control over the server code you should use 'attachement' as this will allow to use the same filename generation code. If you have no control over the server this is a good solution.

Important to note that this does not work across domains (e.g., same-origin policy gets in the way). If downloading from one domain, the download attribute will not work if content is stored on a different domain. CORS may allow that content to pass through (haven't tested).

This is a brilliant solution to the problem however, as always, IE is holding us back from using it: caniuse.com/#search=download

This is literally the opposite of what the OP is asking :)

here filename is optional, but if provided, it will take this name for downloaded file.

Note
Rectangle 27 0

http headers How to force files to open in browser instead of download (pdf)?


<FilesMatch "\.(?i:pdf)$">
    ForceType application/octet-stream
    Header set Content-Disposition attachment
</FilesMatch>
Note
Rectangle 27 0

http headers How to force files to open in browser instead of download (pdf)?


header("Content-Disposition", "inline; filename=myfilename.myextension");
header("Content-Disposition", "attachment; filename=myfilename.myextension");
  • Use the HTML5 download attribute, uses custom filename specified on the view side.

If you have control over the server code:

NOTE: I prefer setting the filename on the server side as you may have more information and can use common code.

No control over the server code:

The correct type is application/pdf for PDF, not application/force-download, this looks like a hack for some legacy browsers. Always use the correct mimetype if you can.

Note
Rectangle 27 0

http headers How to force files to open in browser instead of download (pdf)?


header("Content-Type: application/force-download");
    header("Content-type: application/pdf");
    header("Content-Disposition: inline; filename=\"".$name."\";");

If you don't want the browser to prompt the user then use "inline" for the third string instead of "attachment". Inline works very well. The PDF display immediately without asking the user to click on Open. I've used "attachment" and this will prompt the user for Open, Save. I've tried to change the browser setting nut it doesn't prevent the prompt.

Note