function downloadFile(url: string, fileName: string) {
|
return new Promise<void>((resolve, reject) => {
|
let baseUrl = "";
|
if (process.env.NODE_ENV === "development") {
|
// 跨域请求
|
baseUrl = "http://localhost:8080/bg/";
|
} else {
|
baseUrl = location.protocol + "//" + location.host + "/bg/";
|
}
|
const link = document.createElement("a");
|
link.href = baseUrl + url;
|
// 1. 预检文件是否存在
|
fetch(link.href, { method: "HEAD" })
|
.then(() => {
|
link.download = fileName;
|
document.body.appendChild(link); // 添加到 DOM
|
link.click();
|
window.URL.revokeObjectURL(url);
|
resolve();
|
})
|
.catch(() => {
|
reject();
|
});
|
});
|
}
|
|
export default downloadFile;
|