后台返回文件流,前端进行处理下载文件

.post('xxxxx接口地址')
.send(obj)//需要的参数
.responseType('blob');//重点!!!!!处理文件流

前端页面处理

.then((res) => {
     const blob = res.body;//接口返回的文件流
     const reader = new FileReader();
     reader.readAsDataURL(blob);
     reader.onload = (e) => {
              const a = document.createElement('a');
              a.download = `文件名称.xls`;//看需求是下载什么文件可以改后缀
   // 后端设置的文件名称在res.headers的 "content-disposition": "form-data; 
  //name=\"attachment\"; filename=\"20181211191944.zip\"",
               a.href = e.target.result;
               document.body.appendChild(a);
               a.click();
               document.body.removeChild(a);
     };
});

下载文件的方法还有其他的可参考同类博客:https://blog.csdn.net/Sunny_lxm/article/details/89216977

猜你喜欢

转载自blog.csdn.net/Sunny_lxm/article/details/105490613
今日推荐