前端JS 下载大文件解决方案
问题场景 点击导出按钮,提交请求,下载excel大文件(超过500M),该文件没有预生成在后端, 直接以文件流的形式返回给前端。 解决方案 在Vue项目中常用的方式是通过axios配置请求,读取后端返回的文件流,常用代码如下: axios({ method: 'post', url: 'api/file', responseType: 'blob' }).then(res=> { if (res.data){ filename = 'filename'; let blob = new Blob([res.data],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8"}); if (window.navigator.msSaveOrOpenBlob){ // IE10+下载 navigator.msSaveOrBlob(blob, filename); }else{ // 非IE10+下载 let link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = filename; document.body.appendChild