Force browser to open file instead of prompting download

我的梦境 提交于 2019-12-06 20:14:47

问题


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. I.e., 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!


回答1:


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...




回答2:


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.




回答3:


open the file in a Microsoft Word and save as html.



来源:https://stackoverflow.com/questions/19257238/force-browser-to-open-file-instead-of-prompting-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!