Kaynağa Gözat

txt下载问题

zhangjie 3 yıl önce
ebeveyn
işleme
2bc9e9e34a
2 değiştirilmiş dosya ile 19 ekleme ve 18 silme
  1. 1 1
      src/modules/exam/views/DataTaskManage.vue
  2. 18 17
      src/plugins/utils.js

+ 1 - 1
src/modules/exam/views/DataTaskManage.vue

@@ -80,7 +80,7 @@
           }}</span>
         </el-table-column>
         <el-table-column prop="createName" label="创建人"></el-table-column>
-        <el-table-column class-name="action-column" label="操作" width="100px">
+        <el-table-column class-name="action-column" label="操作" width="160px">
           <template slot-scope="scope">
             <el-button
               v-if="scope.row.hasReportFile && checkPrivilege('link', 'export')"

+ 18 - 17
src/plugins/utils.js

@@ -128,15 +128,15 @@ export async function downloadBlob(fetchFunc, fileName) {
   return true;
 }
 
-function toDataURL(url) {
-  return fetch(url)
-    .then(response => {
-      return response.blob();
-    })
-    .then(blob => {
-      return URL.createObjectURL(blob);
-    });
-}
+// function toDataURL(url) {
+//   return fetch(url)
+//     .then(response => {
+//       return response.blob();
+//     })
+//     .then(blob => {
+//       return URL.createObjectURL(blob);
+//     });
+// }
 
 /**
  *
@@ -154,14 +154,15 @@ export async function downloadFileURL(url, name) {
       .split("?")[0];
   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;
-  }
+  // txt文件会直接在浏览器里面打开。
+  // 方案一:修改服务器设置,加上 Content-Disposition: attachment
+  // 方案二:同源文件a标签添加download属性
+  // if ([".txt"].some(v => fileName.endsWith(v))) {
+  //   link.href = await toDataURL(url);
+  // } else {
+  //   link.href = url;
+  // }
+  link.href = url;
   document.body.appendChild(link);
   link.click();
   document.body.removeChild(link);