前端处理后端返回的文件流生成word并下载

// 发送请求
  this.$axios({
    url:"xxxxxxxxxx你的地址", // 请求地址
    method:'post',  // 请求类型
    data:obj,  // 请求体(参数)
     responseType:'blob',  // 重点 blob
    headers:{' Content-Type': 'application/json'} // 规定发送的格式
  })
  .then(res=>{
      // 接收后端返回
      //规定文件类型 res就是返回的数据流
      let blob = new Blob([res],{ type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=UTF-8' });
      let link = document.createElement('a');
      let objectUrl = URL.createObjectURL(blob);
      link.setAttribute("href",objectUrl);
      link.setAttribute("download",'xxxxx.docx');
      link.click();
      //释放内存
      window.URL.revokeObjectURL(link.href)
    })
    .catch(err=>{
      console.log("文件不存在", err)
    })
posted @ 2021-07-06 15:03  WJJ呀  阅读(1125)  评论(0)    收藏  举报