我有一些大尺寸的PDF目录在我的网站上,我需要链接这些下载。当我在谷歌上搜索时,我发现下面有这样一件事。它应该打开“另存为…”弹出链接点击…
<head>
<meta name="content-disposition" content="inline; filename=filename.pdf">
...
但它不工作:/当我链接到一个文件如下,它只是链接到文件,并试图打开文件。
<a href="filename.pdf" title="Filie Name">File name</a>
更新(根据以下答案):
据我所知,没有100%可靠的跨浏览器解决方案。也许最好的方法是使用下面列出的web服务之一,并提供下载链接…
http://box.net/
http://droplr.com/
http://getcloudapp.com/
这是旧的帖子,但这是一个我的解决方案在JavaScript使用jQuery库。
<script>
(function($){
var download = [];
$('a.force-download, .force-download a').each(function(){
// Collect info
var $this = $(this),
$href = $this.attr('href'),
$split = $href.split('/'),
$name = document.title.replace(/[\W_]/gi, '-').replace(/-{2,}/g, '-'); // get title and clean it for the URL
// Get filename from URL
if($split[($split.length-1)])
{
$tmp = $split[($split.length-1)];
$tmp = $tmp.split('.');
$name = $tmp[0].replace(/[\W_]/gi, '-').replace(/-{2,}/g, '-');
}
// If name already exists, put timestamp there
if($.inArray($name, download) > -1)
{
$name = $name + '-' + Date.now().replace(/[\W]/gi, '-');
}
$(this).attr("download", $name);
download.push($name);
});
}(jQuery || window.jQuery))
</script>
您只需要在<a>标记中使用类force-download,就会自动强制下载。您也可以将它添加到父div,并将拾取其中的所有链接。
例子:
<a href="/some/good/url/Post-Injection_Post-Surgery_Instructions.pdf" class="force-download" target="_blank">Download PDF</a>
这对于WordPress和任何其他系统或自定义网站来说都是很棒的。
Chrome 91有一个新的变化,它支持Chrome 86-90和91+。
下面的语法将使其发生。
const fileHandle = await self.showSaveFilePicker({
suggestedName: 'Untitled Text.txt',
types: [{
description: 'Text documents',
accept: {
'text/plain': ['.txt'],
},
}],
});
点击此处阅读更多信息:
https://developer.chrome.com/blog/new-in-chrome-91/
**另一个解决方案,你可以把它作为一个blob,然后使用savas **
const blob = fetch("some-url-here").then(data => data.blob());
saveAs(blob, "filename.txt")