vue下载or批量下载.mp3文件(亲测-pdf等文件均有效)
下面展示一些 内联代码片
。
// A code block
var foo = 'bar';
// An highlighted block
downRow(index,data){ //单个下载
let that = this
let oReq = new XMLHttpRequest();
oReq.open("GET", data.rows[index].fileServer+'/'+data.rows[index].recordFile, true);
oReq.responseType = "blob";
oReq.onload = function (oEvent) {
var content = oReq.response;
var elink = document.createElement('a');
elink.download = data.rows[index].recordFile.split('/')[3].replace('.mp3','-'+data.rows[index].cdrVar.split(':')[1]+'.mp3');
elink.style.display = 'none';
var blob = new Blob([content])
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
};
oReq.send();
},
batch(){//批量下载
//方式一,服务器牛逼,请用这个
let that = this
this.batchArr.map((item)=>{
let oReq = new XMLHttpRequest();
oReq.open("GET", item.fileServer+'/'+item.recordFile, true);
oReq.responseType = "blob";
oReq.onload = function (oEvent) {
var content = oReq.response;
var elink = document.createElement('a');
elink.download = item.recordFile.split('/')[3].replace('.mp3','-'+item.cdrVar.split(':')[1]+'.mp3');
elink.style.display = 'none';
var blob = new Blob([content])
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
};
oReq.send();
})
//方式二 垃圾服务器,一秒10次,请用这个,哈哈哈哈
let that = this
let num = 5 //下载个数
let time = 10000 //延迟时间
for(let n=0;n<this.batchArr.length/num;n++){
setTimeout(()=>{
this.batchArr.slice(n*num,(n+1)*num).map((item)=>{
let oReq = new XMLHttpRequest();
oReq.open("GET", item.fileServer+'/'+item.recordFile, true);
oReq.responseType = "blob";
oReq.onload = function (oEvent) {
var content = oReq.response;
var elink = document.createElement('a');
elink.download = item.recordFile.split('/')[3].replace('.mp3','-'+item.cdrVar.split(':')[1]+'.mp3');
elink.style.display = 'none';
var blob = new Blob([content])
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
};
oReq.send();
})
},time*n)
}
},
downExcel(){
let that = this
let oReq = new XMLHttpRequest();
oReq.open("POST", that.$my.api+'callBack/exportRecordData', true);
oReq.responseType = "blob";
oReq.setRequestHeader("Content-Type","application/json");
oReq.onload = function (oEvent) {
var content = oReq.response;
var elink = document.createElement('a');
elink.download = '回推excel.xls';
elink.style.display = 'none';
var blob = new Blob([content], { type: 'application/vnd.ms-excel'})
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
};
oReq.send(JSON.stringify({...this.formInline}));
},
···
原文链接: https://blog.csdn.net/qq_25252769/article/details/91045286