3 回答

TA贡献1842条经验 获得超12个赞

TA贡献1871条经验 获得超8个赞
<a href="file link" download target="_blank">Click here to download</a>

TA贡献1772条经验 获得超6个赞
元标记不是实现这一结果的可靠方法。通常,您甚至不应该这样做-应该由用户/用户代理来决定如何处理所提供的内容。如果用户愿意的话,总是可以强迫他们的浏览器下载文件。
如果仍然希望强制浏览器下载文件,请直接修改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'); // Allow support for download resume
header('Content-Length: ' . filesize($path)); // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf'); // Change the 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
请注意,这只是HTTP协议的一个扩展;有些浏览器可能会忽略它。
- 3 回答
- 0 关注
- 760 浏览
相关问题推荐
添加回答
举报