强制浏览器打开文件而不是提示下载 [英] Force browser to open file instead of prompting download

查看:130
本文介绍了强制浏览器打开文件而不是提示下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firefox和Chrome浏览器中点击PDF链接时,该文件有时会在浏览器中查看,有时会提示另存为对话框。



如果我想强制链接始终提示下载,我可以使用下载 HTML5属性。

然而,我想要做相反的事情。即,强制链接始终在浏览器中查看。



反向下载属性的排序。有这样的事吗? :)



我希望在提供PDF文档时不修改响应标题 - 我希望能够在标记中指定浏览器行为应该是什么。



谢谢!

解决方案

您可以通过设置适当的头例如,在PDF的情况下,头部将是 Content-type:application / pdf;



,浏览器将知道该文件的MIME类型,并在与它兼容时显示它。



这里可以看到PDF的标题。

作为提示,我喜欢做什么是使用某种控制器(如果你使用的是后端语言)来处理下载。因此,要下载 myNewProject.pdf 我做了

 < a href ='download.php?file = myNewProject.pdf& viewInBrowser = 1'>下载!< / a> 
$ b> / p>

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!

解决方案

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

这篇关于强制浏览器打开文件而不是提示下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆