49 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-03-04 09:16:33 +08:00
import { ElNotification } from "element-plus";
/**
* @description blob
* @param {Function} api api方法()
* @param {String} tempName ()
* @param {Object} params ()
* @param {Boolean} isNotify ( true)
* @param {String} fileType (.xlsx)
* @return void
* */
export const useDownload = async (
api: (param: any) => Promise<any>,
tempName: string,
params: any = {},
isNotify: boolean = true,
fileType: string = ".xlsx"
) => {
if (isNotify) {
ElNotification({
title: "温馨提示",
message: "如果数据庞大会导致下载缓慢哦,请您耐心等待!",
type: "info",
duration: 3000
});
}
try {
const res = await api(params);
// const blob = new Blob([res], {
// type: "application/vnd.ms-excel;charset=UTF-8"
// });
const blob = new Blob([res]);
// 兼容 edge 不支持 createObjectURL 方法
if ("msSaveOrOpenBlob" in navigator) return window.navigator.msSaveOrOpenBlob(blob, tempName + fileType);
const blobUrl = window.URL.createObjectURL(blob);
const exportFile = document.createElement("a");
exportFile.style.display = "none";
exportFile.download = `${tempName}${fileType}`;
exportFile.href = blobUrl;
document.body.appendChild(exportFile);
exportFile.click();
// 去除下载对 url 的影响
document.body.removeChild(exportFile);
window.URL.revokeObjectURL(blobUrl);
} catch (error) {
console.log(error);
}
};