javascript从路径下载文件
下面是我用来下载文件的示例,但它只在下一个选项卡中打开文件。它不下载文件javascript从路径下载文件,javascript,html,laravel,file,download,Javascript,Html,Laravel,File,Download,下面是我用来下载文件的示例,但它只在下一个选项卡中打开文件。它不下载文件 function download(items) { items.forEach(function (item, index) { // console.log(item); var anchor = document.createElement('a'); anchor.href = item.url; anchor.target = '_blank'; anchor.dow
function download(items) {
items.forEach(function (item, index) {
// console.log(item);
var anchor = document.createElement('a');
anchor.href = item.url;
anchor.target = '_blank';
anchor.download = item.name;
anchor.innerHTML ='download';
setTimeout(function () {
// console.log(anchor);
anchor.click();
}, index * 100);
});
}
“下载”属性仅适用于相同来源的URL
此API调用获取URL并创建可下载的图像您可以共享
item.URL
和item.name
的外观吗?欢迎使用SO…生成了什么URL?你能不能把那个名字改成:“dummy.pdf”,网址是:“1/dummy.pdf”,我现在收到了“访问控制允许来源”错误
fetch("https://upload.wikimedia.org/wikipedia/commons/a/a3/June_odd-eyed-cat.jpg")
.then(response => response.blob())
.then(blob => {
const blobURL = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = blobURL;
a.innerHTML = "download"
a.setAttribute('download', 'true')
document.body.appendChild(a);
})