Yansen 2 år sedan
förälder
incheckning
c529cdf58d
1 ändrade filer med 8 tillägg och 7 borttagningar
  1. 8
    7
      src/utils/request.js

+ 8
- 7
src/utils/request.js Visa fil

@@ -18,9 +18,9 @@ instance.interceptors.request.use(function (config) {
18 18
     successTip,
19 19
     headers: {
20 20
       ...headers,
21
-      responseType: download ? 'blob' : responseType,
22 21
       Authorization: token,
23
-    }
22
+    },    
23
+    responseType: download ? 'blob' : responseType,
24 24
   };
25 25
 }, function (error) {
26 26
   // 对请求错误做些什么
@@ -33,8 +33,8 @@ instance.interceptors.response.use(function (response) {
33 33
   // 对响应数据做点什么
34 34
 
35 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 40
   if (data.code === 1000) {
@@ -128,15 +128,16 @@ export function restful(url) {
128 128
 
129 129
 function downloadBlob(response) {
130 130
   let fileName = '未知文件';
131
+  const contentType = response.headers['content-type'];
131 132
   const contentDisposition = response.headers['content-disposition'];
132 133
   if (contentDisposition) {
133 134
     const parts = contentDisposition.split(';filename=');
134 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 141
   const link = document.createElement('a');
141 142
   link.href = url;
142 143
   link.setAttribute('download', fileName);