Michael Wang 4 жил өмнө
parent
commit
2cfb649f8e
2 өөрчлөгдсөн 25 нэмэгдсэн , 3 устгасан
  1. 20 3
      src/utils/utils.js
  2. 5 0
      vue.config.js

+ 20 - 3
src/utils/utils.js

@@ -41,14 +41,31 @@ export function object2QueryString(obj) {
   return queryString.stringify(obj);
 }
 
+function toDataURL(url) {
+  return fetch(url)
+    .then((response) => {
+      return response.blob();
+    })
+    .then((blob) => {
+      return URL.createObjectURL(blob);
+    });
+}
+
 // 下载文件
-export function downloadFileURL(url, name) {
+export async function downloadFileURL(url, name) {
   const link = document.createElement("a");
   link.style.display = "none";
-  // txt 文件会直接在浏览器里面打开,这时候只能修改服务器设置,加上 Content-Disposition: attachment
-  link.href = url;
   const fileName = name || url.split("/").pop();
   link.setAttribute("download", fileName);
+
+  // txt 文件会直接在浏览器里面打开,这时候只能修改服务器设置,加上 Content-Disposition: attachment
+  if ([".txt"].some((v) => fileName.endsWith(v))) {
+    // const urlObj = new URL(url);
+    // link.href = await toDataURL(url.replace(urlObj.origin));
+    link.href = await toDataURL(url);
+  } else {
+    link.href = url;
+  }
   document.body.appendChild(link);
   link.click();
   document.body.removeChild(link);

+ 5 - 0
vue.config.js

@@ -4,6 +4,11 @@ let proxy = {
     // target: "http://192.168.10.86:6001/",
     changeOrigin: true,
   },
+  "/file": {
+    target: "http://192.168.10.36:6001/",
+    // target: "http://192.168.10.86:6001/",
+    changeOrigin: true,
+  },
 };
 
 var webpack = require("webpack");