zoukankan      html  css  js  c++  java
  • axios 利用new FileReader() 下载文件获取返回的错误信息

    this.axios({
              method: "post",
              url: url,
              data: data,
              responseType: "blob" 
            })
              .then(res => {
                const data = res.data
                let r = new FileReader()
                r.onload = function () {
                  try {
                    let resData = JSON.parse(this.result)
                    console.log(resData)
                    if (resData && resData['code'] && resData['code'] != '1000') {
                     alert(resData.msg);//弹出返回的错误msg
                    }
                  } catch (err) {
                    let fileName = '下载文件名.xls'
                    // 兼容ie11
                    if (window.navigator.msSaveOrOpenBlob) {
                      try {
                        const blobObject = new Blob([data])
                        window.navigator.msSaveOrOpenBlob(blobObject, fileName)
                      } catch (e) {
                        console.log(e)
                      }
                      return
                    }
                   this.download(data, fileName)
                    alert('导出成功')
                  }
                }
                r.readAsText(data) // FileReader的API
              })
              .catch(e => {
                let msg = "网络异常";
                _that.isCanClick = true
                this.$Message.error(msg);
              });
     
     // 下载文件
        download(data, name) {
          if (!data) {
            return;
          }
          let url = window.URL.createObjectURL(new Blob([data]));
          let link = document.createElement("a");
          link.style.display = "none";
          link.href = url;
          link.setAttribute("download", name);
          document.body.appendChild(link);
          link.click();
        },
  • 相关阅读:
    ajax专题
    luogu P1346 电车 最短路
    luogu P1462 通往奥格瑞玛的道路 最短路
    luogu P1328 生活大爆炸版石头剪刀布
    luogu P1315 联合权值 枚举
    luogu P1156 垃圾陷阱 背包问题
    luogu P1217 回文质数 枚举
    luogu P3650 滑雪课程设计 枚举
    luogu1209 修理牛棚 贪心
    luogu P1223 排队接水 贪心
  • 原文地址:https://www.cnblogs.com/jiajiamiao/p/11607598.html
Copyright © 2011-2022 走看看