The interface returns a byte stream, receives the byte stream and saves it to a file in the corresponding format via Blob

Axios ({method: 'post', url: interface address, data: interface parameter, responseType: 'arrayBuffer ', timeout: Then (res => {var blob = new blob ([res.data], {type: type: 'application/vnd.ms-excel; charset=utf-8'}); Var downloadElement = document.createElement('a'); var href = window.URL.createObjectURL(blob); // Create a download link downloadElement.href = href; downloadElement.download = this.exportName + this.formatDate() + '.xlsx'; / / download the file name after the document. The body. The appendChild (downloadElement); downloadElement.click(); / / click to download the document. The body. RemoveChild (downloadElement); / / the download is complete remove elements window. URL. RevokeObjectURL (href); // Release the blob object})Copy the code

Configure files in different formats

  • pdf
blob= new Blob([res.data], {type: 'application/pdf; charset=utf-8'});Copy the code
  • Excel – XLS, XLSX
blob= new Blob([res.data], {type: 'application/vnd.ms-excel; charset=utf-8'});Copy the code
  • Word-doc, docx
blob= new Blob([res.data], {type: 'application/msword; charset=utf-8'});Copy the code
  • Compressed files: zip and RAR
blob= new Blob([res.data], {type: 'application/zip; charset=utf-8'});Copy the code