axios 下载文件请求, responseType为blob时, 无法捕获后端抛出错误问题

image.png
download() {
      if (!this.form.fileType) {
        return this.$message.error('请选择文件类型')
      }
      if (!this.form.transDate) {
        return this.$message.error('请选择交易日期')
      }
      const params = {
        fileType: this.form.fileType,
        transDate: this.form.transDate.split('-').join('')
      }
      this.loading = true
      downloadbillCheckFile(params).then(result => {
        console.log(result)
        downloadFile(result, '对账文件', 'xlsx')
        this.loading = false
      }).catch(error => {
// axios 下载文件请求, responseType为blob时, 无法捕获后端抛出错误问题代码
        if (error.response.data.type === 'application/json') {
          const reader = new FileReader()
          reader.readAsText(error.response.data)
          reader.onload = e => {
            const { message } = JSON.parse(reader.result)
            this.$message.error(message)
          }
        }
        this.loading = false
      })
    }
  }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容