如何强制添加 PDF 链接以告诉浏览器将其下载为 PDF?
我的应用程序生成一个指向与产品关联的任何 PDF 文件的动态链接。该链接的显示方式如下:
<a href="http://mysite.net/mylink.pdf?uid=db5f3dc108594b34a86ad52c8686ade5%2FMyCompany&expires=1324049275&signature=jrSF87xtHkQDCTvOek6uuMacPRc%3D" target="_blank">Brochure</a>
如果用户右键单击并选择“将链接文件下载为”(或其等效项),则该文件在 Google Chrome 和 Safari 中会显示为“.pdf.png”扩展名。 Firefox 可以正常工作,不确定 Internet Explorer 是否可以。
我希望 Firefox 和 Chrome 知道它是 PDF。因为显然用户会尝试下载这些文件,所以他们会使用错误的扩展名保存它,并且他们将无法打开该文件。
My application generates a dynamic link to any PDF files that are associated with a product. The link is presented like this:
<a href="http://mysite.net/mylink.pdf?uid=db5f3dc108594b34a86ad52c8686ade5%2FMyCompany&expires=1324049275&signature=jrSF87xtHkQDCTvOek6uuMacPRc%3D" target="_blank">Brochure</a>
If the user right-clicks and selects "Download Linked File As" (or its equivalent), the file is presented with a ".pdf.png" extension in Google Chrome and Safari. Firefox works appropriately, not sure about Internet Explorer.
I want Firefox and Chrome to know that it is a PDF. Because obviously users are going to try to download these, they are going to save it with the wrong extension, and they won't be able to open the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您使用 Rails 控制器中的“send_data”来提供文件,我可能会建议:
其中“data”是 PDF 的内容。
有关更多信息,请查看以下链接:
http://api.rubyonrails .org/classes/ActionController/DataStreaming.html#method-i-send_data
Assuming you are using "send_data" from within a rails controller to serve the file might I suggest:
Where "data" is the contents of the PDF.
For more information checkout the following link:
http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data
在某些脚本语言(如 php 或 user .htaccess)中发送正确的标头
send proper headers in some scripting lang like php or user .htaccess
您听说过内容处置标头吗?它允许您告诉浏览器询问用户如何处理该文件,而不是尝试自行处理它。我不认为它是 HTTP 规范的一部分,但 IETF 在 RFC 2183 下记录了它。
您应该能够使用您所使用的任何语言在 HTTP 标头到达客户端之前对其进行更改。您添加的标头将如下所示:
您可能还需要一个 Content-Type 标头:
Have you heard of the content-disposition header? It allows you to tell the browser to ask the user what to do with the file, rather than try and handle it by itself. I don't think it is part of the HTTP spec, but it is documented by the IETF under RFC 2183.
You should be able to use whatever language you are using to alter the HTTP headers before they go to the client. The header you add will look something like this:
You might also need a Content-Type header: