Просмотр исходного кода

优化文件下载出现的异常(I6DLNU)

RuoYi лет назад: 3
Родитель
Сommit
7fdd20c054
3 измененных файлов с 8 добавлено и 14 удалено
  1. 3 3
      ruoyi-ui/src/plugins/download.js
  2. 3 3
      ruoyi-ui/src/utils/request.js
  3. 2 8
      ruoyi-ui/src/utils/ruoyi.js

+ 3 - 3
ruoyi-ui/src/plugins/download.js

@@ -15,9 +15,9 @@ export default {
15 15
       url: url,
16 16
       responseType: 'blob',
17 17
       headers: { 'Authorization': 'Bearer ' + getToken() }
18
-    }).then(async (res) => {
19
-      const isLogin = await blobValidate(res.data);
20
-      if (isLogin) {
18
+    }).then((res) => {
19
+      const isBlob = blobValidate(res.data);
20
+      if (isBlob) {
21 21
         const blob = new Blob([res.data], { type: 'application/zip' })
22 22
         this.saveAs(blob, name)
23 23
       } else {

+ 3 - 3
ruoyi-ui/src/utils/request.js

@@ -72,7 +72,7 @@ service.interceptors.response.use(res => {
72 72
     // 获取错误信息
73 73
     const msg = errorCode[code] || res.data.msg || errorCode['default']
74 74
     // 二进制数据则直接返回
75
-    if(res.request.responseType ===  'blob' || res.request.responseType ===  'arraybuffer'){
75
+    if (res.request.responseType ===  'blob' || res.request.responseType ===  'arraybuffer') {
76 76
       return res.data
77 77
     }
78 78
     if (code === 401) {
@@ -125,8 +125,8 @@ export function download(url, params, filename, config) {
125 125
     responseType: 'blob',
126 126
     ...config
127 127
   }).then(async (data) => {
128
-    const isLogin = await blobValidate(data);
129
-    if (isLogin) {
128
+    const isBlob = blobValidate(data);
129
+    if (isBlob) {
130 130
       const blob = new Blob([data])
131 131
       saveAs(blob, filename)
132 132
     } else {

+ 2 - 8
ruoyi-ui/src/utils/ruoyi.js

@@ -228,12 +228,6 @@ export function tansParams(params) {
228 228
 }
229 229
 
230 230
 // 验证是否为blob格式
231
-export async function blobValidate(data) {
232
-  try {
233
-    const text = await data.text();
234
-    JSON.parse(text);
235
-    return false;
236
-  } catch (error) {
237
-    return true;
238
-  }
231
+export function blobValidate(data) {
232
+  return data.type !== 'application/json'
239 233
 }