【问题标题】:Force save files all browsers - not open in browser window在所有浏览器中强制保存文件 - 不在浏览器窗口中打开
【发布时间】:2012-04-03 16:07:50
【问题描述】:

我正在寻找一个适用于所有浏览器的简单解决方案。

对于特定文件类型或通过类的目标链接:我怎样才能让它们在所有主要浏览器中简单地强制下载。

我认为我找到了 apachce 服务器的完美解决方案 - 将其添加到 .htaccess 中。

http://css-tricks.com/snippets/htaccess/force-files-to-download-not-open-in-browser/

AddType application/octet-stream .csv
AddType application/octet-stream .xls
AddType application/octet-stream .doc
AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .pdf

似乎可以在 Firefox 和 Safari 中使用,但不能在 chrome 或 IE 中使用(尚未测试其他任何东西)

谁能帮助我解决如何创建链接以强制下载文件,而不是在浏览器中打开,适用于所有浏览器。

我似乎找不到完整的浏览器证明解决方案。不可能吗?

任何指向教程或 sn-ps 的链接都很棒。

如果我的网站基于 PHP,那么如果可能的话,它可以与 PHP 一起使用。

谢谢

【问题讨论】:

    标签: php download


    【解决方案1】:

    将 Content-Disposition 标头设置为“附件”,如下所示(在 PHP 中):

    header('Content-Disposition: attachment');
    

    您甚至可以为下载建议一个文件名(如果它与 URL 中给出的不同):

    header('Content-Disposition: attachment; filename=file.ext');
    

    如果您想直接在 Apache 服务器中执行此操作,请尝试以下操作:

    <FilesMatch "\.(pdf|csv|xls)">
      Header set Content-Disposition attachment
    </FilesMatch>
    

    (您可以在 FilesMatch 模式中添加更多扩展)

    如果可以,请尽量避免使用 application/octet-stream 标头,也就是说,如果 Content-Disposition 没有它也可以工作。那是因为有些系统实际上会注意 Content-Type 并将其与文件一起保存。如果他们的系统能够确定文件的正确 MIME 类型,通常对用户来说会更好。

    【讨论】:

    • 您好,感谢您的帮助。我将此添加到我的 wordpress 网站。在我的 single-download.php 模板(这是我希望强制下载发生的地方)上,我将您的行添加到模板中,如下所示...&lt;?php /** * @package WordPress * @subpackage MyTheme */ get_header(); header('Content-Disposition: attachment'); ?&gt; 但似乎没有效果?谢谢
    • 确保在向浏览器输出任何内容之前运行所有 header() 调用。如果 get_header() 函数输出任何内容,请将您的 header() 调用放在此之前。
    【解决方案2】:
    <FilesMatch "\.(?i:pdf)$">
      ForceType application/octet-stream
      Header set Content-Disposition attachment
    </FilesMatch>
    

    将此添加到 .htaccess(将 pdf 更改为您想要的任何内容)- 目前在我测试过的所有浏览器中都可以使用(尚未测试过 Opera 或 safari)

    【讨论】:

      【解决方案3】:

      假设$file 保存了您文件的路径,这是我经常用来执行此操作的脚本:

      $file = 'file.gif'; // example
      $filesize=filesize($file);
      
      if ($filesize > 0) {
      header("Pragma: public"); // required
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: private",false); // required for certain browsers 
      header("Content-Type: image/gif");
      header("Content-Length: ".filesize($file));
      header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
      
      readfile($file);
      }
      

      您确实会遇到没有文件结构的设备(例如 iPad/iPhone)的问题,但您可以删除 Content-Disposition 行来解决这个问题。

      【讨论】:

      • 谢谢,您的回答似乎很有趣。我认为我可以在 wordpress 的基本循环中轻松完成这项工作,但我的 PHP 知识非常基础。我正在尝试通过厨房短代码使其工作... do_shortcode('[gallery customgal="true"]'); 由此自定义画廊功能生成... gist.github.com/2300437 - 我想强制下载的 $fullsize_image 变量,用于第 108 行。任何指针将不胜感激谢谢。乔什
      猜你喜欢
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多