简体   繁体   中英

Force browser to open file instead of prompting download

When clicking a PDF link in Firefox and Chrome, the file will sometimes be opened for in-browser viewing and sometimes prompt a "Save as" dialog.

If I wanted to force the link to always prompt a download I could use the download HTML5 attribute.

However, I want to do the opposite. Ie, force the links to always be viewed in the browser .

Sort of an inverse download attribute. Is there such a thing? :)

I'd prefer to not modify response headers when serving PDF documents - I want to be able to specify in markup what the browser behavior should be.

Thanks!

You can achieve that by setting the appropriate header (for instance, in case of PDF, the header will be Content-type: application/pdf;

With this header, the browser will know the mime-type of the file and display it if it is compatible with it.

Here you can see the headers for a PDF.
As a hint, what I like to do is to use some sort of controller (in case you are using a backend language) that handles the download. Hence, to download myNewProject.pdf I do

<a href='download.php?file=myNewProject.pdf&viewInBrowser=1'>Download!</a>

Then I can set the appropriate headers depending on the file type, or if I want to force download or view it in the browser...

I'm using Firefox in XP. I went to the OPTIONS under Tools and found Portable Document Format. Click on it and it will allow you to change the way PDF files are handled.

在Microsoft Word中打开该文件并另存为html。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM