【问题标题】:Force PDF file download using htaccess使用 htaccess 强制下载 PDF 文件
【发布时间】:2014-11-19 04:02:49
【问题描述】:

我使用 Apache 作为后端服务器,使用 nginx 作为前端服务器。我需要使 PDF 文件可下载(此时它们正在浏览器窗口中打开)。

这是一个链接:

<a href="http://domain.com/files/teltomat.pdf" target="">link</a>

这是我迄今为止在 .htaccess 文件中尝试过的内容:

<FilesMatch "\.(pdf)$">
   ForceType application/octet-stream
   Header set Content-Disposition attachment
</FilesMatch>

没用,只是在浏览器中打开文件。

AddType application/force-download pdf

没用。

AddType application/octet-stream .pdf

没用。

更新

已尝试:wget --server-response -O /dev/null http://domain.com/files/teltomat.pdf

得到了回应:

HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Server: nginx
  Date: Wed, 24 Sep 2014 17:40:54 GMT
  Content-Type: application/pdf
  Content-Length: 3116445
  Last-Modified: Wed, 24 Sep 2014 13:28:07 GMT
  Connection: keep-alive
  Keep-Alive: timeout=60
  ETag: "5422c6e7-2f8d9d"
  Expires: Thu, 31 Dec 2037 23:55:55 GMT
  Cache-Control: max-age=315360000
  Accept-Ranges: bytes
Length: 3116445 (3,0M) [application/pdf]
Saving to: ‘/dev/null’

【问题讨论】:

  • 我认为这与 Laravel 没有任何关系。
  • 您是否验证了标头是否已进入浏览器,例如与wget --server-response -O /dev/null http://domain.com/files/teltomat.pdf 或Windows 等效? IME,浏览器不会嗅探内容。

标签: apache .htaccess pdf


【解决方案1】:

您可以尝试添加“下载”而不是“目标”的 HTML5 解决方案:

<a href="http://domain.com/files/teltomat.pdf" download="teltomat.pdf">link</a>

看起来服务器端正在做正确的事情(通过使处置“附件”),但也许浏览器自己决定它可以处理 PDF 的内联并打开一个新窗口。

【讨论】:

    猜你喜欢
    • 2012-02-23
    • 2016-01-16
    • 2011-08-12
    • 2011-05-12
    • 2013-03-31
    • 2012-05-21
    • 2020-04-28
    • 1970-01-01
    • 2014-12-27
    相关资源
    最近更新 更多