Sfoglia il codice sorgente

使用uuid+timestamp+random当文件名

chauncey 10 mesi fa
parent
commit
1882d31878

+ 8 - 1
src/views/disaster/disaster-control/PageDisposalRectificationItem.vue

@@ -98,8 +98,15 @@
   };
   const formatFileList = async (file: FileItem) => {
     if (!file.file) return file;
+    const uuid = Math.random().toString(36).substring(2, 9);
+    const timestamp = Date.now().toString();
+    const random = Math.random().toString(36).substring(2, 4);
     const fileName = file.fileName;
-    const res = await uploadFileApi({ bizType: UPLOAD_BIZ_TYPE.ATTACHMENT, fileName, file: file.file });
+    const res = await uploadFileApi({
+      bizType: UPLOAD_BIZ_TYPE.ATTACHMENT,
+      fileName: `${uuid}-${timestamp}-${random}`,
+      file: file.file,
+    });
     const fileType = file.fileType;
     const fileSize = file.fileSize;
     const fileId = file.fileId;

+ 45 - 45
src/views/disaster/utils/download.ts

@@ -3,54 +3,54 @@
  * @param url 文件url
  * @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);
+// };