1:
export async function downloadFile(exportRecordId) {
try {
let result = await exportApi.getDownloadFile({ exportRecordId });
return result;
} catch (error) {
// axios错误处理,页面走catch
if (error.response) {
return Promise.reject(error.response);
}
if (error.type === "application/zip") {
const a = document.createElement("a");
const href = window.URL.createObjectURL(error);
a.href = href;
a.download = `xxx.zip`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a); // 下载完成移除元素
window.URL.revokeObjectURL(href);
return { code: 200, data: null };
} else {
let data = await error.text();
return JSON.parse(data);
}
}
}
2:
拦截器:
putFile: (url) => {
return instance.get(url, { responseType: "blob" });
},
/**
* 导出Excel
* @param name 文件名
* @param data 二进制流
*/
export const exprotExcal = (name, data) => {
const Link = new Blob([data], {
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8",
});
const fileName = name + ".xlsx";
const a = document.createElement("a");
a.download = fileName;
a.href = URL.createObjectURL(Link);
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(a.href);
document.body.removeChild(a);
};