|
|
@@ -8,16 +8,16 @@ export const downloadFile = (url: string | undefined, name: string): void => {
|
|
|
// 创建XMLHttpRequest请求
|
|
|
const request = new XMLHttpRequest();
|
|
|
request.responseType = "blob";
|
|
|
-
|
|
|
+
|
|
|
// 打开请求
|
|
|
request.open("GET", encodeURI(url));
|
|
|
-
|
|
|
+
|
|
|
// 设置onload回调函数
|
|
|
request.onload = function () {
|
|
|
if (this.status === 200) {
|
|
|
// 创建Blob URL
|
|
|
const blobUrl = window.URL.createObjectURL(this.response);
|
|
|
-
|
|
|
+
|
|
|
// 创建a标签
|
|
|
const link = document.createElement('a');
|
|
|
// 设置下载链接
|
|
|
@@ -36,12 +36,21 @@ export const downloadFile = (url: string | undefined, name: string): void => {
|
|
|
window.URL.revokeObjectURL(blobUrl);
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
// 添加错误处理
|
|
|
request.onerror = function() {
|
|
|
console.error('下载文件失败');
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
// 发送请求
|
|
|
request.send();
|
|
|
};
|
|
|
+// export const downloadFile = (url: string | undefined, name: string): void => {
|
|
|
+// if (!url) return;
|
|
|
+// const link = document.createElement('a');
|
|
|
+// link.href = url;
|
|
|
+// link.download = name;
|
|
|
+// document.body.appendChild(link);
|
|
|
+// link.click();
|
|
|
+// document.body.removeChild(link);
|
|
|
+// };
|