我的页面上有一个PDF文件的链接。
Safari浏览器会直接在网站中打开这个PDF文件,但我想要下载它。
我已经尝试给<a>
元素添加target="_blank"
,但如果在浏览器设置中禁用弹出窗口,则无效。
我应该如何解决这个问题?
谢谢。
Content-Disposition
设置为attachment
,以便下载该文件。Content-Disposition: attachment; filename=test.pdf
根据您使用的Web服务器,配置方式可能会有所不同。另一种选择是使用服务器端脚本来流式传输PDF并设置此标题。
AddType application/octet-stream .pdf
您需要使用服务器端脚本(如PHP)动态地强制附加头文件。以下是使用PHP的示例:
<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>
编辑:
$file = $_GET['file'];
readfile($file."pdf"); // Before doing this, check whather the user have permission to download that file.
调用你的脚本:download.php?file=document
将会下载document.pdf
<a href>
来下载它。 - shamittomar