Axios sets the response type, gets blob, filename, 1. Download the A label and destroy the A label. 2. Download the A label from Internet Explorer

axios({
  method:'... '.url:'... '.// 'responseType' indicates the data type of the server response, which can be 'arrayBuffer ',' blob', 'document', 'json','text', 'stream'.
  responseType:'blob'
}).then(res= > {
    const {headers, data} = res,
    * If network can see Content-disposition, code can't get it, back-end configuration is required
    filename = decodeURI(headers["content-disposition"].split("filename=") [1]),
    blob = new Blob([data], {type: headers["content-type"]});
    saveAs(blob, filename)
})

// Save to the local directory
function saveAs(blob,filename) {
    if ('download' in document.createElement('a')) {
         const eleA = document.createElement('a');
         eleA.download = fileName
         eleA.style.display = 'none';
         eleA.href = URL.createObjectURL(blob);
         document.body.appendChild(eleA);
         eleA.click();
         URL.revokeObjectURL(eleA.href); // Release the URL object
         document.body.removeChild(eleA);
   } else {
       / / IE to download
       navigator.msSaveOrOpenBlob(blob, filename)
   }
}
Copy the code