import request from '@/router/axios';
import {baseUrl,baseUrlNew} from '@/config/env';
import { Message, Loading } from 'element-ui'
/**
* 下载压缩包
*/
export const downloadZipGener = (params, url) => {
let loadingInstance = Loading.service({
text: "正在下载数据,请稍候...",
background: "rgba(0, 0, 0, 0.7)",
});
return request({
url: baseUrl + url + params,
method: "get",
responseType: "blob",
})
.then((res) => {
if (res.data.type == "application/json") {
const reader = new FileReader();
reader.readAsText(res.data, "utf-8");
reader.onload = function () {
const { data } = JSON.parse(reader.result);
//弹出错误提示
Message({
message: data,
type: "warning",
dangerouslyUseHTMLString: true,
});
};
} else {
const blob = new Blob([res.data], {
type: "application/zip",
});
let link = document.createElement("a");
let body = document.querySelector("body");
link.href = window.URL.createObjectURL(blob);
link.style.display = "none";
const headerFilename = res.headers["content-disposition"]
?.split(";")[1]
.split("=")[1];
const fileNameHeader = decodeURIComponent(headerFilename);
link.download = fileNameHeader;
body.appendChild(link);
link.click();
body.removeChild(link);
window.URL.revokeObjectURL(link.href);
}
loadingInstance.close();
})
.catch((err) => {
console.log(err);
loadingInstance.close();
});
};
vue+element+axios下载zip文件接收二进制,区分其他返回类型进行错误提示
最新推荐文章于 2024-11-13 11:52:56 发布