vue 中单个下载和批量下载
最近开发项目需要做文件下载。感觉挺简单的,就是后端传过来下载的URL地址,然后点击下载,不多这个也分多个下载和单个下载。多个下载比较麻烦一点,不说了,上代码
<el-button v-if="del==true" size="mini" type="text" class="deleteBtn" icon="el-icon-download" @click="handledownload(scope.row)" title="删除"></el-button>
我的值这个是子组件传过来的,你们也可以异步请求过来,只要获取它的url地址就可以
先说单个下载
这个是一个vue调用的方法
handledownload(row){
console.log("row",row)
// // this.datafile ='./static/htm'
// this.datafile =row.fileUrl
// // console.log("href: " + link.href);
if(row.fileUrl){
window.open(row.fileUrl)
}else{
this.$message('该会议未上传文档');
}
},
但是这个只能用单个下载,不能支持多个下载,因为浏览器规则原因。
这个可以多个下载,我也是在网上看到的
handleBatchdownload(){
this.selectedRows = this.$refs.myPage.selectedRows //这个里用的element的组件
console.log(this.selectedRows);//选择下载几个文件
var delData = []
for(var i=0;i<this.selectedRows.length;i++){//循环出来下载url
delData.push(this.selectedRows[i].fileUrl);//数组里面
this.downloadFile(delData[i]);//调用方法,加入参数
}
console.log(delData)
},
downloadFile(url){
const iframe = document.createElement("iframe");
iframe.style.display = "none"; // 防止影响页面
iframe.style.height = 0; // 防止影响页面
iframe.src = url;
document.body.appendChild(iframe);
setTimeout(()=>{
iframe.remove();
}, 5 * 60 * 1000);
this.$refs.myPage.selectedRows = []
},
好了,到这里就结束,上面的方法也有借鉴网上 。