导出---后台返回二进制流文件数据,前端转换格式进行下载

isExcel(type,name, data) {
  const link = document.createElement('a')
  const blob = new Blob([data])
  link.style.display = 'none'
  link.href = URL.createObjectURL(blob)
  link.setAttribute('download', `${name}.`+type)
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
},
fuDown() {
      let parmas = {
        fileName: this.blogData.annexUrl.split("/")[
          this.blogData.annexUrl.split("/").length - 1
        ],
      };

      downloadFu(parmas)//集成的axios
        .then((res) => {
          console.log("下载");
          console.log(res);
          console.log("res");
          this.$tool.isExcel(this.type, this.typeName, res);
        })
        .catch((res) => {
          console.log(res.msg);
          console.log("res.msg");
        });

    //原生axios//
// axios // .get("http://172.16.9.224:8090/blogs/blog/download", { // params: { // fileName: this.blogData.annexUrl.split("/")[ this.blogData.annexUrl.split("/").length - 1 ], //请求参数,文件名称 // }, // headers: { // 'X-Token': '1635c0cbc5f64e63b4d8f2bb171db48d#ACCOUNT#89177' // }, // responseType: 'blob', //二进制流 ,请求时需要配置响应类型 // }) // .then((res) => { // console.log("数据是:", res); // this.$tool.isExcel(this.type,this.typeName,res.data) type文件类型、typename 文件名称 data返回的二进制流数据

// }) // .catch((e) => { // console.log("获取数据失败"); // }); },

 补充方法:

export function buildExport(name, url, params = {}) {
  console.log("传入的参数-------------",params,(process.env.VUE_APP_API_URL?process.env.VUE_APP_API_URL:configs.baseURL.target) + url)
  axios({
    method: 'post',
    url: (process.env.VUE_APP_API_URL?process.env.VUE_APP_API_URL:configs.baseURL.target) + url,
    data:  {param:encryptList.Encrypt(JSON.stringify(params))},
    responseType: 'blob',
    headers: { 'token': tool.getToken(),appId:tool.getStorage("appId") }
  }).then(res => {
    const link = document.createElement('a')
    const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
    link.style.display = 'none'
    link.href = URL.createObjectURL(blob)
    link.setAttribute('download', `${name}.xls`)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link);
  }).catch(err => {
    console.log('导出报错', err);
  });
}

 

posted @ 2020-10-16 19:07  张小中  阅读(1994)  评论(0编辑  收藏  举报