【问题标题】:How to force PDF to download beginner [duplicate]如何强制PDF下载初学者[重复]
【发布时间】:2012-08-13 00:33:28
【问题描述】:

可能重复:
How to force a pdf download automatically?

我查过其他论坛,但我不明白他们在说什么。我检查了这个链接,人们似乎确实得到了解决方案:

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

我想做一个用于下载 PDF 的按钮,文件太大而无法在线查看。所以我希望该按钮强制浏览器下载文件。询问或不询问用户。会有不同的 PDF 名称文件,所以我需要使这个按钮可以下载各种 PDF 文件的 PDF 格式。

我检查了不同的网站,似乎可以使用 htaccess 完成,但老实说,我不知道如何做到这一点。有人可以帮我看看我怎么能做到这一点!?!?!有没有通过 CSS/html 的简单方法??

为了这么一个小细节,我已经纠结了很久了。

我的代码很简单:

<div class="Container for icons">
<a href="hugefile.pdf" alt=""><img href="icon.png" alt=""/>
</div>

有人可以帮我看看这个吗?

【问题讨论】:

    标签: php pdf header force-download


    【解决方案1】:

    您需要修改返回给客户端的 HTTP 标头才能完成此操作:

    这是一个 PHP 代码示例:

    $path = "path/to/file.pdf";
    $filename = "file.pdf";
    header('Content-Transfer-Encoding: binary');  // For Gecko browsers mainly
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
    header('Accept-Ranges: bytes');  // For download resume
    header('Content-Length: ' . filesize($path));  // File size
    header('Content-Encoding: none');
    header('Content-Type: application/pdf');  // Change this mime type if the file is not PDF
    header('Content-Disposition: attachment; filename=' . $filename);  // Make the browser display the Save As dialog
    readfile($path);  //this is necessary in order to get it to actually download the file, otherwise it will be 0Kb
    

    【讨论】:

      【解决方案2】:

      你可以试试ForceType,而不是AddType

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

      【讨论】:

      • 注意:这是一个 Apache 配置
      猜你喜欢
      • 2015-02-11
      • 2019-05-30
      • 2022-10-15
      • 1970-01-01
      • 2013-07-04
      • 2017-04-11
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多