2014-09-24 105 views
0

我使用Apache作为后端服务器和nginx作为前端服务器。我需要下载PDF文件(此时它们在浏览器窗口中打开)。使用htaccess强制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 

没有工作。

UPDATE

尝试: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’ 
+0

我不认为这与Laravel有任何关系。 – will 2014-09-24 15:34:29

+0

您是否验证了标题可以将其传送到浏览器,例如:用'wget --server-response -O/dev/null http:// domain.com/files/teltomat.pdf'或者Windows的等价物? IME,浏览器不会内容嗅探。 – 2014-09-24 15:54:26

+0

更新了我的答案。 – Heihachi 2014-09-24 17:43:01

回答

2

你可以尝试将 “下载”,而不是 “目标” 的HTML5解决方案:

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

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