js 下载文件

window.open()只能打开一次,所以下载多个文件只能下载一个
function download(name, href) {
var a = document.createElement("a"), //创建a标签
e = document.createEvent("MouseEvents"); //创建鼠标事件对象
e.initEvent("click", false, false); //初始化事件对象
a.href = href; //设置下载地址
a.download = name; //设置下载文件名
a.dispatchEvent(e); //给指定的元素,执行事件click事件
}

下载多个文件的思路的创建多个iframe,且不能马上删除
var sites = ['url','url2']
for (let i = 0; i < sites.length; i++) {
const iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.style.height = 0;
// url自己进行指定
iframe.src = sites[i];
document.body.appendChild(iframe);
// 不能马上将iframe进行删除,否者也会出现马上取消的情况
setTimeout(() => {
iframe.remove();
}, 3000);
}

猜你喜欢

转载自www.cnblogs.com/shihx/p/12467246.html
今日推荐