vue3下载文件流 下载excel
文件下载
·
//点击下载按钮
qaGetDebitVoucher:async (id:any)=>{
let res=await $axios.get($api.qaGetDebitVoucher+id,{ responseType: "blob",})
download(res);
},
// 下载文件
const download = (res: any) => {
const data = res.data;
if (!res.data) {
return;
}
// 设置下载文件名称,使用正则取出名称
const pat = new RegExp("(?<=filename=).*");
let contentDisposition = "";
//浏览器问题可能会出现 content-disposition 匹配不到
if (res.headers["content-disposition"]) contentDisposition = res.headers["content-disposition"];
if (res.headers["Content-Disposition"]) contentDisposition = res.headers["Content-Disposition"];
const result = pat.exec(contentDisposition);
let fileName = result && result[0];
if (fileName == undefined) {
fileName = "扣款凭证.xlsx";
} else {
fileName = decodeURIComponent(fileName);
}
let url = window.URL.createObjectURL(new Blob([data]));
let link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
};
下载成功
有的request没有配置的需要去配置一下
if (response.status === 200) {
// return Promise.resolve(response.data);
//下载文件流
if (response.config.responseType === 'blob') {
return response;
}
// 如果请求错误,统一处理
if (response.data.success) {
return response;
} else {
message.warn(response.data.errMessage);
}
} else {
Request.errorHandle(response);
// return Promise.reject(response.data);
return response;
}
更多推荐
已为社区贡献1条内容
所有评论(0)