文件流导出文件
// 下载excel
export const downloadFile = (fileStream, fileName) => {if (window.navigator.msSaveBlob) {try {window.navigator.msSaveBlob(fileStream, fileName);} catch (e) {console.log(e);}} else {const url = window.URL || window.webkitURL;const bUrl = url.createObjectURL(fileStream);let a = document.createElement("a");a.style.display = "none";a.href = bUrl;a.download = decodeURIComponent(fileName);document.body.appendChild(a);a.click();document.body.removeChild(a);url.revokeObjectURL(bUrl);}
};
使用downloadFile(res, `默认文件.xlsx`);
res是后端返回文件流,默认文件.xlsx是文件名 .xlsx是后缀名
// 获取文件类型
export function getFileType(file) {const type = file.type || "";const name = file.name || "";if (type.includes("word")) {return "doc";} else if (type.includes("excel") || type.includes("spreadsheetml.sheet")) {return "excel";} else if (type === "text/plain") {return "txt";} else if (type === "application/pdf") {return "pdf";} else if (type === "text/html") {return "html";} else if (type === "text/markdown" || name.includes(".md")) {return "md";} else {return "other";}
}