如何强制添加 PDF 链接以告诉浏览器将其下载为 PDF?

发布于 2024-12-21 19:39:49 字数 496 浏览 0 评论 0原文

我的应用程序生成一个指向与产品关联的任何 PDF 文件的动态链接。该链接的显示方式如下:

<a href="http://mysite.net/mylink.pdf?uid=db5f3dc108594b34a86ad52c8686ade5%2FMyCompany&amp;expires=1324049275&amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪菊怪哟 2024-12-28 19:39:49

假设您使用 Rails 控制器中的“send_data”来提供文件,我可能会建议:

send_data(
    data, 
    :filename => "filename.pdf",
    :disposition => "attachment",
    :type => 'application/pdf'
  )

其中“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:

send_data(
    data, 
    :filename => "filename.pdf",
    :disposition => "attachment",
    :type => 'application/pdf'
  )

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

一刻暧昧 2024-12-28 19:39:49

在某些脚本语言(如 php 或 user .htaccess)中发送正确的标头

<Files *.pdf>
  ForceType application/pdf
  Header set Content-Disposition attachment
</Files>

send proper headers in some scripting lang like php or user .htaccess

<Files *.pdf>
  ForceType application/pdf
  Header set Content-Disposition attachment
</Files>
少跟Wǒ拽 2024-12-28 19:39:49

您听说过内容处置标头吗?它允许您告诉浏览器询问用户如何处理该文件,而不是尝试自行处理它。我不认为它是 HTTP 规范的一部分,但 IETF 在 RFC 2183 下记录了它

您应该能够使用您所使用的任何语言在 HTTP 标头到达客户端之前对其进行更改。您添加的标头将如下所示:

Content-Disposition: attachment; filename=filename.pdf

您可能还需要一个 Content-Type 标头:

Content-Type: application/pdf

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:

Content-Disposition: attachment; filename=filename.pdf

You might also need a Content-Type header:

Content-Type: application/pdf
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文