瀏覽代碼

文件下载

asd26269546 1 年之前
父節點
當前提交
f9904da470
共有 3 個文件被更改,包括 27 次插入32 次删除
  1. 18 30
      src/utils/request.js
  2. 2 2
      src/views/process/processApproval/index.vue
  3. 7 0
      vite.config.js

+ 18 - 30
src/utils/request.js

@@ -153,37 +153,25 @@ service.interceptors.response.use(res => {
   }
 )
 // 通用下载方法
-export function download(url, params, filename, config) {
-  downloadLoadingInstance = ElLoading.service({
-    text: "正在下载数据,请稍候",
-    background: "rgba(0, 0, 0, 0.7)",
-  })
-  return service.post(url, params, {
-    transformRequest: [(params) => {
-      return tansParams(params)
-    }],
-    headers: {
-      'Content-Type': 'application/x-www-form-urlencoded'
-    },
-    responseType: 'blob',
-    ...config
-  }).then(async (data) => {
-    const isLogin = await blobValidate(data);
-    if (isLogin) {
-      const blob = new Blob([data])
-      saveAs(blob, filename)
-    } else {
-      const resText = await data.text();
-      const rspObj = JSON.parse(resText);
-      const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
-      ElMessage.error(errMsg);
+export function download(fileUrl, fileName) {
+  let xhr = new XMLHttpRequest();
+  //域名是华为云的
+  xhr.open("GET", `${fileUrl}`, true);
+  xhr.responseType = "blob";
+  xhr.send();
+  xhr.onreadystatechange = function () {
+    if (xhr.readyState === 4 && xhr.status === 200) {
+      let url = window.URL.createObjectURL(xhr.response);
+      const a = document.createElement("a");
+      a.href = url;
+      a.download = fileName; // 下载后文件名
+      a.style.display = "none";
+      document.body.appendChild(a);
+      a.click(); // 点击下载
+      window.URL.revokeObjectURL(a.href);
+      document.body.removeChild(a); // 下载完成移除元素
     }
-    downloadLoadingInstance.close();
-  }).catch((r) => {
-    console.error(r)
-    ElMessage.error('下载文件出现错误,请联系管理员!')
-    downloadLoadingInstance.close();
-  })
+  };
 }
 
 

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

@@ -92,7 +92,8 @@
                   </div>
                   {{ item.remark }}
                   <div v-for="j in fileObj[item.flowExampleDetailId]" v-if="fileObj[item.flowExampleDetailId]">
-                    <a :href="j.fileUrl" style="color:#409EFF;line-height: 30px;">{{ j.fileName }}</a>
+                    <!-- :href="j.fileUrl" -->
+                    <a  @click="proxy.download(j.fileUrl, j.fileName)" style="color:#409EFF;line-height: 30px;">{{ j.fileName }}</a>
                   </div>
                 </div>
               </div>
@@ -238,7 +239,6 @@ const handleResult = (res) => {
   }
 };
 
-
 const uploadFile = async (file) => {
   const res = await proxy.post("/fileInfo/getSing", { fileName: file.name });
   uploadData.value = res.uploadBody;

+ 7 - 0
vite.config.js

@@ -44,6 +44,13 @@ export default defineConfig(({
           // target:"http://139.159.251.109/prod-api",
           changeOrigin: true,
           rewrite: (p) => p.replace(/^\/dev-api/, '')
+        },
+        '/byteSailing': {
+          target: 'https://os.winfaster.cn/byteSailing',
+          // 正式地址
+          // target:"http://139.159.251.109/prod-api",
+          changeOrigin: true,
+          rewrite: (p) => p.replace(/^\/byteSailing/, '')
         }
       }
     },