request({
method: "post",
responseType: "blob",
url: '/views/openView',
data:{
view_id:this.row.view_id,
download:true
}
}).then(res=>{
if (res.data.type === 'application/json') { //判断是否json
let reader = new FileReader()
reader.readAsText(res.data, 'utf-8')
reader.onload = (e) => {
// JSON.parse(reader.result) 转译后的json数据
res.data = JSON.parse(reader.result)
console.log(res.data);
// 逻辑处理...
return this.$notify({ position: 'bottom-right',
title:'提示',
message:res.data.msg,
type:'warning'
})
}
}else {
// 文件流,下载文件逻辑...
// 下载文件
let blob = new Blob([res.data]);
let fileName = res.headers['content-disposition'].split('=')[res.headers['content-disposition'].split('=').length-1]
if(fileName.indexOf("UTF-8") != -1){
fileName = decodeURI(fileName.split("'")[2])
}
let elink = document.createElement("a");
// 设置下载文件名
elink.download = fileName;
elink.style.display = "none";
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
// this.$loading().close()
this.$notify({ position: 'bottom-right',
title:'提示',
message:'下载成功!请查看已下载文件',
type:'success'
})
}
后端接口返回blod格式数据处理
最新推荐文章于 2023-05-20 14:13:02 发布