Html 强制浏览器打开文件而不是提示下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19257238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force browser to open file instead of prompting download
提问by Ted Nyberg
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.
在 Firefox 和 Chrome 中单击 PDF 链接时,文件有时会打开以在浏览器中查看,有时会提示“另存为”对话框。
If I wanted to force the link to always prompt a download I could use the downloadHTML5 attribute.
如果我想强制链接总是提示下载,我可以使用下载HTML5 属性。
However, I want to do the opposite. I.e., force the links to always be viewed in the browser.
但是,我想做相反的事情。即,强制始终在浏览器中查看链接。
Sort of an inverse downloadattribute. 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.
我希望在提供 PDF 文档时不修改响应标头 - 我希望能够在标记中指定浏览器行为应该是什么。
Thanks!
谢谢!
回答by Nico
You can achieve that by setting the appropriate header (for instance, in case of PDF, the header will be Content-type: application/pdf;
您可以通过设置适当的标题(例如,在 PDF 的情况下,标题将是 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.
使用此标头,浏览器将知道文件的 MIME 类型,并在与它兼容时显示它。
Hereyou 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
在这里您可以看到 PDF 的标题。
作为提示,我喜欢做的是使用某种控制器(如果您使用的是后端语言)来处理下载。因此,下载myNewProject.pdf
我做
<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...
然后我可以根据文件类型设置适当的标题,或者如果我想强制下载或在浏览器中查看它...
回答by Bob
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.
我在 XP 中使用 Firefox。我去了工具下的选项,找到了便携式文档格式。单击它,它将允许您更改处理 PDF 文件的方式。
回答by Yash
open the file in a Microsoft Word and save as html.
在 Microsoft Word 中打开文件并另存为 html。