如何强制PDF下载初学者


How to force PDF to download beginner

可能的重复项:
如何强制自动下载pdf?

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

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>

有人可以帮我这个吗?

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

  • 强制打开"另存为..." 弹出窗口在文本链接处打开 点击 HTML 中的 PDF

下面是一个 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

与其AddType,不如试试ForceType

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