Yansen 2 gadus atpakaļ
vecāks
revīzija
c529cdf58d
1 mainītis faili ar 8 papildinājumiem un 7 dzēšanām
  1. 8
    7
      src/utils/request.js

+ 8
- 7
src/utils/request.js Parādīt failu

18
     successTip,
18
     successTip,
19
     headers: {
19
     headers: {
20
       ...headers,
20
       ...headers,
21
-      responseType: download ? 'blob' : responseType,
22
       Authorization: token,
21
       Authorization: token,
23
-    }
22
+    },    
23
+    responseType: download ? 'blob' : responseType,
24
   };
24
   };
25
 }, function (error) {
25
 }, function (error) {
26
   // 对请求错误做些什么
26
   // 对请求错误做些什么
33
   // 对响应数据做点什么
33
   // 对响应数据做点什么
34
 
34
 
35
   const { data, config } = response;
35
   const { data, config } = response;
36
-  if (config.download) {
37
-    return downloadBlob(response.data, '下载文件');
36
+  if (config.download && !data.code) {
37
+    return downloadBlob(response, '下载文件');
38
   }
38
   }
39
   
39
   
40
   if (data.code === 1000) {
40
   if (data.code === 1000) {
128
 
128
 
129
 function downloadBlob(response) {
129
 function downloadBlob(response) {
130
   let fileName = '未知文件';
130
   let fileName = '未知文件';
131
+  const contentType = response.headers['content-type'];
131
   const contentDisposition = response.headers['content-disposition'];
132
   const contentDisposition = response.headers['content-disposition'];
132
   if (contentDisposition) {
133
   if (contentDisposition) {
133
     const parts = contentDisposition.split(';filename=');
134
     const parts = contentDisposition.split(';filename=');
134
     if (parts[1]) {
135
     if (parts[1]) {
135
-      fileName = parts[1];
136
+      fileName = decodeURIComponent(parts[1]);
136
     }
137
     }
137
   }
138
   }
138
-
139
-  const url = window.URL.createObjectURL(response.data);
139
+console.log(response);
140
+  const url = window.URL.createObjectURL(new Blob([response.data], { type: contentType }));
140
   const link = document.createElement('a');
141
   const link = document.createElement('a');
141
   link.href = url;
142
   link.href = url;
142
   link.setAttribute('download', fileName);
143
   link.setAttribute('download', fileName);