Html 在浏览器中打开文件而不是下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7267242/
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
Opening files in browser instead of downloading
提问by kurupt_89
I have a href link to a pdf file that when clicked on it downloads. I want this to just open in a new page instead of downloading.
我有一个指向 pdf 文件的 href 链接,单击该文件会下载该文件。我希望它只是在新页面中打开而不是下载。
<a href="../file/1012">clicking here</a>
I've had a look at a website that does this with iframe, but I would prefer not to use that.
我看过一个使用 iframe 执行此操作的网站,但我不想使用它。
采纳答案by kmcc049
add target="_blank" to get a new window. adding .pdf may help. If you have control over the host web server adjusting the headers for the files in question will also help it open inline. I can tell you what headers to set if you are able too
添加 target="_blank" 以获取新窗口。添加 .pdf 可能会有所帮助。如果您可以控制主机 Web 服务器,调整相关文件的标题也将有助于它内联打开。如果您也可以,我可以告诉您要设置哪些标题
回答by BornToCode
Your server should return this http header:
您的服务器应返回此 http 标头:
Content-Type: application/pdf
For viewing in browser it should also return:
为了在浏览器中查看,它还应该返回:
Content-Disposition: inline;filename="myfile.pdf"
For downloading:
下载:
Content-Disposition: attachment;filename="myfile.pdf"
回答by tripleee
The server tells the browser whether the content you are getting is "inline" or not. If you are controlling the server, you will want to change the server's settings for whether to serve the problematic file as an attachment. Otherwise, there are browser plug-ins which allow you to override this. For Firefox, there is Open in Browser; http://spasche.net/openinbrowser/
服务器会告诉浏览器您获取的内容是否“内联”。如果您正在控制服务器,您将需要更改服务器的设置,以决定是否将有问题的文件作为附件提供。否则,有浏览器插件允许您覆盖它。对于 Firefox,有在浏览器中打开;http://spasche.net/openinbrowser/
回答by svanelten
You can make the link open in a new window by adding target="_blank"
. The question if the PDF file is opened in the window or downloaded depends on the software the user has installed, i don't think it is possible to control that.
您可以通过添加在新窗口中打开链接target="_blank"
。PDF文件是在窗口中打开还是下载的问题取决于用户安装的软件,我认为无法控制。