|
@@ -3,54 +3,54 @@
|
|
|
* @param url 文件url
|
|
* @param url 文件url
|
|
|
* @param name 文件名
|
|
* @param name 文件名
|
|
|
*/
|
|
*/
|
|
|
-// export const downloadFile = (url: string | undefined, name: string): void => {
|
|
|
|
|
-// if (!url) return;
|
|
|
|
|
-// // 创建XMLHttpRequest请求
|
|
|
|
|
-// const request = new XMLHttpRequest();
|
|
|
|
|
-// request.responseType = "blob";
|
|
|
|
|
|
|
+export const downloadFile = (url: string | undefined, name: string): void => {
|
|
|
|
|
+ if (!url) return;
|
|
|
|
|
+ // 创建XMLHttpRequest请求
|
|
|
|
|
+ const request = new XMLHttpRequest();
|
|
|
|
|
+ request.responseType = "blob";
|
|
|
|
|
|
|
|
-// // 打开请求
|
|
|
|
|
-// request.open("GET", encodeURI(url));
|
|
|
|
|
|
|
+ // 打开请求
|
|
|
|
|
+ request.open("GET", encodeURI(url));
|
|
|
|
|
|
|
|
-// // 设置onload回调函数
|
|
|
|
|
-// request.onload = function () {
|
|
|
|
|
-// if (this.status === 200) {
|
|
|
|
|
-// // 创建Blob URL
|
|
|
|
|
-// const blobUrl = window.URL.createObjectURL(this.response);
|
|
|
|
|
|
|
+ // 设置onload回调函数
|
|
|
|
|
+ request.onload = function () {
|
|
|
|
|
+ if (this.status === 200) {
|
|
|
|
|
+ // 创建Blob URL
|
|
|
|
|
+ const blobUrl = window.URL.createObjectURL(this.response);
|
|
|
|
|
|
|
|
-// // 创建a标签
|
|
|
|
|
-// const link = document.createElement('a');
|
|
|
|
|
-// // 设置下载链接
|
|
|
|
|
-// link.href = blobUrl;
|
|
|
|
|
-// // 设置下载文件名
|
|
|
|
|
-// link.download = name;
|
|
|
|
|
-// // 设置链接样式为不可见
|
|
|
|
|
-// link.style.display = 'none';
|
|
|
|
|
-// // 添加到文档中
|
|
|
|
|
-// document.body.appendChild(link);
|
|
|
|
|
-// // 触发点击事件执行下载
|
|
|
|
|
-// link.click();
|
|
|
|
|
-// // 下载完成后移除该元素
|
|
|
|
|
-// document.body.removeChild(link);
|
|
|
|
|
-// // 释放blob URL
|
|
|
|
|
-// window.URL.revokeObjectURL(blobUrl);
|
|
|
|
|
-// }
|
|
|
|
|
-// };
|
|
|
|
|
|
|
+ // 创建a标签
|
|
|
|
|
+ const link = document.createElement('a');
|
|
|
|
|
+ // 设置下载链接
|
|
|
|
|
+ link.href = blobUrl;
|
|
|
|
|
+ // 设置下载文件名
|
|
|
|
|
+ link.download = name;
|
|
|
|
|
+ // 设置链接样式为不可见
|
|
|
|
|
+ link.style.display = 'none';
|
|
|
|
|
+ // 添加到文档中
|
|
|
|
|
+ document.body.appendChild(link);
|
|
|
|
|
+ // 触发点击事件执行下载
|
|
|
|
|
+ link.click();
|
|
|
|
|
+ // 下载完成后移除该元素
|
|
|
|
|
+ document.body.removeChild(link);
|
|
|
|
|
+ // 释放blob URL
|
|
|
|
|
+ window.URL.revokeObjectURL(blobUrl);
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
-// // 添加错误处理
|
|
|
|
|
-// request.onerror = function() {
|
|
|
|
|
-// console.error('下载文件失败');
|
|
|
|
|
-// };
|
|
|
|
|
|
|
+ // 添加错误处理
|
|
|
|
|
+ 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);
|
|
|
|
|
|
|
+ // 发送请求
|
|
|
|
|
+ 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);
|
|
|
|
|
+// };
|