js 批量下载文件
<script>
funcion downLoad(fileUrls){//fileUrls是個數組,存放下載文件的路徑,類似["http://111.dwg", "http://222.docx"......]var main = document.getElementById("main");//隨便獲取一個頁面上的div,Id要與括號內相同fileUrls.forEach((url,index) => {//使用了閉包,返回的函數能夠使用外部的pathconst path = url;var timer1 = setTimeout(function(path){return function(){//定義一個看不見的iframeconst iframe = document.createElement("iframe");iframe.style.display = "none"; // 防止影響頁面設置不可見iframe.style.height = "0"; // 防止影響頁面高度設置為0iframe.src = path;//下載地址main.appendChild(iframe);// 這一行必須,iframe掛在到dom樹上才會發請求var timer2 = setTimeout(function(){iframe.remove();clearTimeout(timer2)},5000) //這個就根據實際情況定義一下延時刪除添加的iframe,不刪除也問題不大clearTimeout(timer1);}
}(path), 1000 * index) //每隔1s插入一個iframe//下面這種方法對于多個文件,只能下載最后一個,所以批量下載不適用,單個文件下載沒問題;
/*var main = document.getElementById("main");a.setAttribute("style", "display:none;");main.appendChild(a); // 修復firefox中無法觸發clicka.setAttribute("download", model.FileUrl);a.setAttribute("href", model.FileUrl);a.click();main.removeChild(a);
*/});
}
</script>
總結
- 上一篇: FastReport Mono 2023
- 下一篇: 安卓初学者笔记(四):用白话讲明白Act