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

时间:2022-10-22 14:44:13

My application generates a dynamic link to any PDF files that are associated with a product. The link is presented like this:

我的应用程序生成与产品关联的任何PDF文件的动态链接。链接如下所示:

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

如果用户右键单击并选择“将链接文件下载为”(或其等效文件),则会在Google Chrome和Safari中为该文件提供“.pdf.png”扩展名。 Firefox工作正常,不确定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.

我希望Firefox和Chrome知道它是PDF。因为显然用户会尝试下载这些内容,他们将使用错误的扩展名保存它们,并且他们将无法打开该文件。

3 个解决方案

#1


3  

Assuming you are using "send_data" from within a rails controller to serve the file might I suggest:

假设您在rails控制器中使用“send_data”来提供文件,我建议:

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

Where "data" is the contents of the PDF.

其中“数据”是PDF的内容。

For more information checkout the following link:

有关更多信息,请查看以下链接:

http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data

http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data

#2


0  

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

在像php或用户.htaccess这样的脚本语言中发送正确的标题

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

#3


0  

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.

你听说过内容处理标题吗?它允许您告诉浏览器询问用户如何处理文件,而不是尝试自己处理它。我不认为它是HTTP规范的一部分,但它由IETF根据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:

在转到客户端之前,您应该能够使用您正在使用的任何语言来更改HTTP标头。您添加的标题将如下所示:

Content-Disposition: attachment; filename=filename.pdf

You might also need a Content-Type header:

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

Content-Type: application/pdf

#1


3  

Assuming you are using "send_data" from within a rails controller to serve the file might I suggest:

假设您在rails控制器中使用“send_data”来提供文件,我建议:

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

Where "data" is the contents of the PDF.

其中“数据”是PDF的内容。

For more information checkout the following link:

有关更多信息,请查看以下链接:

http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data

http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data

#2


0  

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

在像php或用户.htaccess这样的脚本语言中发送正确的标题

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

#3


0  

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.

你听说过内容处理标题吗?它允许您告诉浏览器询问用户如何处理文件,而不是尝试自己处理它。我不认为它是HTTP规范的一部分,但它由IETF根据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:

在转到客户端之前,您应该能够使用您正在使用的任何语言来更改HTTP标头。您添加的标题将如下所示:

Content-Disposition: attachment; filename=filename.pdf

You might also need a Content-Type header:

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

Content-Type: application/pdf