目录

后端返回的文件流,前端实现下载

目录

后端返回的文件流,前端实现下载

后端返回文件流,前端实现word下载


axios.post(url, data, { responseType: "blob" }).then(
  (res) => {
  if (res) {
        let a = document.createElement('a');
        // 通过 blob 对象获取对应的 url
        let url = window.URL.createObjectURL(new Blob([res], { type: 'application/msword;charset=utf-8' }));
        a.href = url;
        a.download = '文件名称';
        a.click();
        a.remove();
      }
    },
  (err) => {
    console.log(err);
  }).catch(err => {
  console.log(err.data)
});

可能会遇到的问题: 下载的文件出现乱码情况,就需要注意responseType 就要这样设置{ responseType: “blob” }

扩展:下载文件的类型可参考该链接: