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 @ 2022-08-29 10:15  DL·Coder  阅读(320)  评论(0编辑  收藏  举报