blob 下载文件后,获取后台错误信息

  • this 在函数里面获取不到,所以要取出来。

  • FileReader 对象允许 Web 应用程序异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容,使用 File 或 Blob 对象指定要读取的文件或数据。

  • FileReader.readAsText() :开始读取指定的Blob中的内容。一旦完成,result属性中将包含一个字符串以表示所读取的文件内容。

this.$api.getReliabilityReportWord(data).then((res) => {
  const _this = this;
  let fileReader = new FileReader();
  fileReader.onload = function () {
    try {
      let jsonData = JSON.parse(this.result); // 说明是普通对象数据,后台转换失败
      if (jsonData.code) {
        console.log(jsonData, "jsonData");
        _this.$message.error(jsonData.message);
      }
    } catch (err) {
      // 解析成对象失败,说明是正常的文件流
      let blob = new Blob([res]);
      let downloadElement = document.createElement("a");
      let href = window.URL.createObjectURL(blob);
      downloadElement.href = href;
      downloadElement.download = `可靠性文档计算书.docx`;
      document.body.appendChild(downloadElement);
      downloadElement.click();
      document.body.removeChild(downloadElement);
      window.URL.revokeObjectURL(href);
    }
  };
  fileReader.readAsText(res);
});
posted @   DL·Coder  阅读(350)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示