现在有了HTML5,可以在<a>元素中设置"download"属性来实现下载。
<a href="http://link/to/file" download="FileName">Download it!</a>
来源: http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
if ("download" in document.createElement("a")){ ... }
- pmrotuledownload
属性可以处理这个问题。你需要使用一个PHP脚本(或其他服务器端语言)来实现此功能。
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
并使用htaccess重定向(重写)到PHP文件,而不是PDF文件。
http://www.example.com/_PDFs/___download_the_catalogue_instead_opening.php
并尝试打开pdf文件时,它仍然在浏览器中打开原始文件,而不是下载重命名的版本... - ellockie标签
。Response.AddHeader("Content-disposition", "attachment; filename=" + Name);
请看这个例子:
http://www.codeproject.com/KB/aspnet/textfile.aspx
这是针对ASP.NET的。我相信你可以在所有其他服务器端语言中找到类似的解决方案。然而,据我所知,没有JavaScript解决方案。
var path= "your file path will be here";
var save = document.createElement('a');
save.href = filePath;
save.download = "Your file name here";
save.target = '_blank';
var event = document.createEvent('Event');
event.initEvent('click', true, true);
save.dispatchEvent(event);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
在新的Chrome更新中,有时事件不起作用。为此,可以使用以下代码:
var path= "your file path will be here";
var save = document.createElement('a');
save.href = filePath;
save.download = "Your file name here";
save.target = '_blank';
document.body.appendChild(save);
save.click();
document.body.removeChild(save);
在 Firefox 和 Internet Explorer 浏览器中,添加子元素和删除子元素非常有用。但是在 Chrome 中,无需添加和删除子元素即可正常工作。
如果没有html5属性,可以使用php来实现:
创建名为download.php的php文件,并使用以下代码:
<?php
ob_start();
$file = "yourPDF.pdf"
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
}
<script>window.location = "download.php";</script>
<a href='javascript:window.location = "download.php"'>
Download it!
</a>
$file = $_GET['$file'];
,因为有人可以通过 download.php?file=../../../wp-config.php
或其他资源来调用。额外的检查文件扩展名、MIME类型和允许下载的路径(并去除如"../"等不必要内容)非常重要! - Giel Berkers<a href='javascript...'>
和直接在 <a href='download.php'>
中放置链接完全相同? - allan.simon我曾经尝试过使用创建a
DOM元素并设置download
属性的方法解决问题,但在某些浏览器中仍会弹出警告(也许它们在2021年变得更加严格了)。
将pdf的mime类型添加到href
属性中可以解决浏览器弹窗警告,但却使文件损坏无法打开。
在2021年,您可以使用Edhowler建议的XMLHttpRequest
来下载PDF文件,而不需要PHP或Apache设置并且没有浏览器警告。他的代码示例使用npm库,但以下是如何只使用js完成该操作:
/**
* Download a file without browser popup warning
* @param {string} url The url of the file to download
* @param {string} filename Set a new filename for the downloaded file (optional)
*/
const downloadFile = (url, filename = '') => {
if (filename.length === 0) filename = url.split('/').pop();
const req = new XMLHttpRequest();
req.open('GET', url, true);
req.responseType = 'blob';
req.onload = function () {
const blob = new Blob([req.response], {
type: 'application/pdf',
});
const isIE = false || !!document.documentMode;
if (isIE) {
window.navigator.msSaveBlob(blob, filename);
} else {
const windowUrl = window.URL || window.webkitURL;
const href = windowUrl.createObjectURL(blob);
const a = document.createElement('a');
a.setAttribute('download', filename);
a.setAttribute('href', href);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
};
req.send();
};
downloadFile('https://domain.tld/file.pdf');
如果需要支持不支持 ES6+ 的浏览器,请使用像 Babel 这样的转译器。
location.href = data
。这将导致浏览器下载该文件。不幸的是,您无法以这种方式设置文件名或扩展名。调整媒体类型可能会产生一些效果。你可以使用 download.js (https://github.com/rndme/download 和 http://danml.com/download.html)。如果文件在外部URL中,你必须进行Ajax请求,但如果不是,那么你可以使用该函数:
download(Path, name, mime)
阅读他们在GitHub上的文档以获取更多详细信息。