fetch请求文件流并下载(Excel)


 

1、blob文件流

fetch(url,{
    method: 'get',
    responseType: 'blob'
}).then(res => {     
    return res.blob();
}).then(blob => {
    let bl = new Blob([blob], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
    let fileName = '文件名'+".xlsx";
    var link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    link.download = fileName;
    link.click();
    window.URL.revokeObjectURL(link.href);
})

 

2、arraybuffer文件流

把上面的blob改成arraybuffer就好了

 

fetch(url,{
    method: 'get',
    responseType: 'arraybuffer'
}).then(res => {     
    return res. arraybuffer();
}).then(arraybuffer => {
    let bl = new Blob([arraybuffer], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
    let fileName = '文件名'+".xlsx";
    var link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    link.download = fileName;
    link.click();
    window.URL.revokeObjectURL(link.href);
})

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM