Quellcode durchsuchen

fix requests.js

zhanghe vor 6 Jahren
Ursprung
Commit
6ea231b57b
2 geänderte Dateien mit 19 neuen und 18 gelöschten Zeilen
  1. 1 2
      src/routes/Dashboard/Accounts/AccountsCampus.js
  2. 18 16
      src/utils/request.js

+ 1 - 2
src/routes/Dashboard/Accounts/AccountsCampus.js

@@ -23,11 +23,10 @@ export default class CampusAccountsPage extends Component {
     this.props.dispatch({
       type: 'accounts/fetchCampusList',
       payload: { ...this.state.Queryers }
-    })
+    });
   }
 
   handleDownloadOperation = () => {
-    console.log('press download');
     this.props.dispatch({
       type: 'accounts/fetchCampusExcel',
       payload: 'download',

+ 18 - 16
src/utils/request.js

@@ -58,7 +58,6 @@ export default function request(url, options) {
     credentials: 'include',
   };
   const newOptions = { ...defaultOptions, ...options };
-  console.log('>>>>>>>>>> 01newOptions',newOptions)
   if (newOptions.method === 'POST' || newOptions.method === 'PUT' || newOptions.method === 'DELETE') {
     if (!(newOptions.body instanceof FormData)) {
       newOptions.headers = {
@@ -76,26 +75,29 @@ export default function request(url, options) {
         'Content-Type': 'multipart/form-data',
         ...newOptions.headers,
       };
-    } 
+    }
   }
-  // download excel
-  if ( newOptions.DOWNLOAD ) {
-    console.log('download request');
-    delete newOptions.DOWNLOAD;
-    newOptions.headers = {
-      Accept: 'application/binary',
-      'Content-Type': 'multipart/x-www-form-urlencoded',
-      ...newOptions.headers,
-    };
-  } 
-  console.log('>>>>>>>>>newOptions',newOptions)
   return fetch(url, newOptions)
     .then(checkHttpStatus)
     .then((response) => {
-      if (response.status === 204) {
-        return response.text();
+      if (response.headers.get('Content-Type') !== 'application/json;charset=UTF-8') {
+        response.blob().then(
+          (blob) => {
+            const a = document.createElement('a');
+            const fakeUrl = window.URL.createObjectURL(blob);
+            const filename = response.headers.get('Content-Disposition').split('filename=')[1];
+            a.href = fakeUrl;
+            a.download = filename;
+            a.click();
+            window.URL.revokeObjectURL(url);
+          });
+        return null;
+      } else {
+        if (response.status === 204) {
+          return response.text();
+        }
+        return response.json();
       }
-      return response.json();
     })
     .then(checkContentStatus)
     .catch((e) => {