今天有一个选择多个文件,然后一起下载的需求
解决方法:通过setTimeOut,有延时的一个个添加iframe标签,
wordArr = [1,2,3] //这里的数据是选中文件的Id,用于拼接地址
wordArr.forEach(function(item,index){
// 拼接成后端定义好的单个文件的下载的路径(根据实际情况)
var path = interUrl.basic + "common/export?templateId=" + item + "&bizId=" + litigationId
//使用了闭包,返回的函数能够使用外部的path
var timer1 = setTimeout(function(path){
return function(){
//定义一个看不见的iframe
var iframe =$("<iframe class='downloadIfream' src='"+path+"' style='height:0;display:none'></iframe>")
$("body").append(iframe)
var timer2 = setTimeout(function(){
iframe.remove()
clearTimeout(timer2)
},5000) //这个就根据实际情况定义一下延时删除添加的iframe,不删除也问题不大
clearTimeout(timer1)
}
}(path), 1000 * index) //每隔1s插入一个iframe
})
单个文件下载:
a标签 模拟点击事件
window.location.href = 下载接口地址
window.open(“下载接口地址”)
都可以实现,比较简单