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);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM