瀏覽代碼

文件下载bug

cz 1 年之前
父節點
當前提交
30466b2fec
共有 4 個文件被更改,包括 24 次插入3 次删除
  1. 2 0
      src/main.js
  2. 19 0
      src/utils/request.js
  3. 1 1
      src/views/process/processApproval/index.vue
  4. 2 2
      vite.config.js

+ 2 - 0
src/main.js

@@ -20,6 +20,7 @@ import directive from './directive' // directive
 import plugins from './plugins' // plugins
 import {
   download,
+  downloadFile,
   post,
   postTwo,
   get
@@ -86,6 +87,7 @@ app.config.globalProperties.get = get
 app.config.globalProperties.post = post
 app.config.globalProperties.postTwo = postTwo
 app.config.globalProperties.download = download
+app.config.globalProperties.downloadFile = downloadFile
 app.config.globalProperties.parseTime = parseTime
 app.config.globalProperties.resetForm = resetForm
 app.config.globalProperties.handleTree = handleTree

+ 19 - 0
src/utils/request.js

@@ -198,6 +198,25 @@ export function download(fileUrl, fileName) {
   };
 }
 
+export function downloadFile(fileUrl, fileName) {
+  fetch(fileUrl)
+    .then(response => response.blob())
+    .then(blob => {
+      const url = window.URL.createObjectURL(blob);
+      const a = document.createElement("a");
+      a.href = url;
+      a.download = fileName;
+      a.style.display = "none";
+      document.body.appendChild(a);
+      a.click();
+      window.URL.revokeObjectURL(url);
+      document.body.removeChild(a);
+    })
+    .catch(error => {
+      console.error("下载文件时发生错误:", error);
+    });
+}
+
 
 export function get(url, params = {}) {
   return new Promise((resolve, reject) => {

+ 1 - 1
src/views/process/processApproval/index.vue

@@ -162,7 +162,7 @@
                   >
                     <!-- :href="j.fileUrl" -->
                     <a
-                      @click="proxy.download(j.fileUrl, j.fileName)"
+                      @click="downloadFile(j.fileUrl, j.fileName)"
                       style="color: #409eff; line-height: 30px"
                       >{{ j.fileName }}</a
                     >

+ 2 - 2
vite.config.js

@@ -39,9 +39,9 @@ export default defineConfig(({
       proxy: {
         // https://cn.vitejs.dev/config/#server-proxy
         '/dev-api': {
-          target: 'http://139.9.102.170:10006/test-api',
+          // target: 'http://139.9.102.170:10006/test-api',
           // 正式地址
-          // target: "http://139.9.102.170/prod-api",
+          target: "http://139.9.102.170/prod-api",
           changeOrigin: true,
           rewrite: (p) => p.replace(/^\/dev-api/, '')
         }