About file download stream is compatible with Firefox problem ~

import request from 'superagent'
 
act Download () {
// request parameters carried
const obj={
system_id:window.localStorage.getItem('system_id'),
reportinfo_ids:this.state.selectedRowsItems
}
request
.get ( '/ laboratory / reportPrint / batchDownloadReport') // get request method
.set ( 'Content-Type', 'multipart / form-data') // setup request header
.set('accesstoken', window.localStorage.getItem('accesstoken'))  //设置token
.query (obj) // parameter
.responseType ( 'blob') // bold binary
.then(function(response){
const blob = new Blob ([response.body], {type: 'application / octet-stream'}); // specified type
const fileName = response.headers.filename // download file name
if(response.statusCode === 200){
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
const aLink = document.createElement('a');
document.body.appendChild(aLink);
aLink.style.display='none';
const objectUrl = window.URL.createObjectURL(blob);
aLink.href = objectUrl;
aLink.download = fileName;
aLink.click();
document.body.removeChild(aLink);
}
 
}else{
message.warn ( 'download failed, please log in again')
}})
.catch(err => {
console.log('err',err) });
}
 
 

react on compatible file stream downloads

. response.blob () the then (BLOB => {
const ALink = document.createElement ( 'A');
document.body.appendChild (ALink);
aLink.style.display = 'none';
const = window.URL the ObjectURL. createObjectURL (BLOB);
aLink.href = the ObjectURL;
aLink.download = fileName;
aLink.click ();
document.body.removeChild (ALink); // the message is for Firefox, without words, Firefox simply can guide no file, Firefox for a.download seems a little personal opinion, but Chrome can toss for a long time.
});

Guess you like

Origin www.cnblogs.com/jiadaxiadedaimashenghuo/p/11001963.html