下载zip文件流当遇到下载失败的时候,就需要将后台错误信息返回给用户看,但是responseType: "blob"格式默认转为二进制。所以当下载错误的时候需要转为json格式,拿到code将错误信息返回给用户.
不多说了,直接上代码:
.then(result => {
(result)
if ( == "text/html") {
const reader = new FileReader();
= function() {
const { msg } = (); //此处的msg就是后端返回的msg内容
that.$message({
message: msg,
type: "warning"
});
};
(result);
} else if( == "application/zip") {
const blob = result;
const reader = new FileReader();
(blob);
= e => {
const a = ("a");
= `商品清单.zip`;
// 后端设置的文件名称在的 "content-disposition": "form-data; name=\"attachment\"; filename=\"\"",
= ;
(a);
();
(a);
};
}