美文网首页
axios 下载文件请求, responseType为blob时

axios 下载文件请求, responseType为blob时

作者: 贞贞姐 | 来源:发表于2021-03-15 14:05 被阅读0次
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
      })
    }
  }

相关文章

网友评论

      本文标题:axios 下载文件请求, responseType为blob时

      本文链接:https://www.haomeiwen.com/subject/qgjkcltx.html