js批量上传文件
原创直接上代码
funcion downLoad(fileUrls){//fileUrls是保```java
< script > funcion downLoad(fileUrls) { //fileUrls是保存下载文件路径的数组,类似。["http://111.dwg", "http://222.docx"......] var main = document.getElementById("main"); //获取任何页面div,Id与括号中的相同 fileUrls.forEach((url, index) = >{ //对于闭包,返回的函数可以使用外部path const path = url; var timer1 = setTimeout(function(path) { return function() { //定义不可见iframe const iframe = document.createElement("iframe"); iframe.style.display = "none"; // 防止页面设置不可见 iframe.style.height = "0"; // 防止设置页面高度0 iframe.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无法在中触发click a.setAttribute("download", model.FileUrl); a.setAttribute("href", model.FileUrl); a.click(); main.removeChild(a); */
});
}
var main = document.getElementById("main");//获取任何页面div,Id与括号中的相同
fileUrls.forEach((url,index) => {
//对于闭包,返回的函数可以使用外部path
const path = url;
var timer1 = setTimeout(function(path){
return function(){
//定义不可见iframe
const iframe = document.createElement("iframe");
iframe.style.display = "none"; // 防止页面设置不可见
iframe.style.height = "0"; // 防止设置页面高度0
iframe.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无法在中触发click
a.setAttribute("download", model.FileUrl);
a.setAttribute("href", model.FileUrl);
a.click();
main.removeChild(a);
*/
});
}
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除