强制浏览器下载文件而不是打开 [英] Force browsers to download a file rather than open
问题描述
我正在使用典型的PHP代码下载文档:
I'm using typical PHP code to download documents:
header('Content-Type: ' . $mimeTypes[$fileext]);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Cache-Control: private');
header('Pragma: private');
readfile($filepath);
一切正常 - 下载/保存对话框打开,但.doc文件尝试在docs.google.com中打开,但由于缺少许可而失败 - 这是因为我正在从网站根外部提供文件。
Everything works fine - a download/save-as dialog opens, except for .doc files which attempt to open in docs.google.com but they fail due to lack of permission - that's because I'm serving files from outside a website root.
那么如何绕过docs.google并强制每个浏览器提供保存为对话框,无论文件MIME类型如何? ('doc'=>'application / msword')
So how do I bypass docs.google and force every browser to offer save-as dialog regardless of the file mime type? ('doc' => 'application/msword')
我尝试以下操作无效: 。/ p>
I tried the following to no avail:
-
in .htaccess file:
in .htaccess file:
<FilesMatch "\.(?i:doc)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
在.htaccess文件中:
in .htaccess file:
AddType application/octet-stream .doc
在PHP脚本中:
in PHP script:
header('Content-Type: application/force-download');
推荐答案
添加到您的标题:
header("Content-type: application/force-download");
这篇关于强制浏览器下载文件而不是打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!