關於文件流下載兼容火狐問題~


import request from 'superagent'
 
handleDownload(){
//請求攜帶的參數
const obj={
system_id:window.localStorage.getItem('system_id'),
reportinfo_ids:this.state.selectedRowsItems
}
request
.get('/laboratory/reportPrint/batchDownloadReport')  //get請求方式
.set('Content-Type', 'multipart/form-data')   //設置請求頭
.set('accesstoken', window.localStorage.getItem('accesstoken'))  //設置token
.query(obj)   //參數
.responseType('blob')  //bold二進制
.then(function(response){
const blob=new Blob([response.body], {type: 'application/octet-stream'});  //指定類型
const fileName = response.headers.filename  //下載文件名
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('下載失敗,請重新登錄')
}})
.catch(err => {
console.log('err',err) });
}
 
 

react 關於文件流下載的兼容

response.blob().then(blob => {
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); //這句是針對火狐的,沒有這句話,火狐就根本導不出文件來,火狐對a.download似乎有點個人意見,但Chrome可以,折騰了好久。
});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM