Blob格式转json格式,拿到后端返回的json数据

75 篇文章 17 订阅
48 篇文章 3 订阅

Blob格式转json格式,拿到后端返回的json数据

在进行文件下载的时候,我们通过blob,进行下载文件,需要axios设置responseType

responseType: 'blob',
 axios({
      	...xxx,
        responseType: 'blob',
    }).then(res => {
          if (!res || !res.headers['content-disposition']) {
               if (res && res.status === 200 && res.data) {

                   //转换步骤
                   const file = new FileReader();
                   file.readAsText(res.data, 'utf-8');
                   file.onload = function () {
                       const obj = JSON.parse(file.result);
                       console.log(obj, 'obj')
                       if (obj.code === '10000') {
                           message.error(obj.message || '下载失败!')
                       }
                       return obj;//此时message为转化好的json格式
                   }
                   return false


               } else {
                   message.error(res.message || '下载失败!')
                   return
               }
           }
           if (res && res.status === 200 && res.data) {
               const { data, headers } = res
               let fileName
               if (headers['content-disposition']) {
                   fileName = headers['content-disposition'].replace(/\w+;filename=(.*)/, '$1')
               } else {
                   fileName = data.fileName
               }
               // 此处当返回json文件时需要先对data进行JSON.stringify处理,其他类型文件不用做处理
               const blob = new Blob([data], { type: headers['content-type'] })
               const dom = document.createElement('a')
               const downUrl = window.URL.createObjectURL(blob)
               dom.href = downUrl
               dom.download = decodeURIComponent(fileName)
               dom.style.display = 'none'
               document.body.appendChild(dom)
               dom.click()
               dom.parentNode.removeChild(dom)
               window.URL.revokeObjectURL(url)
               return res
           }
           message.error(res.message || '下载失败!')
       })
       .catch(err => {
           // error(err || '下载失败!')
           console.log('下载失败')
       })

但是有时候文件过大或者其他原因,后端不让我们下载,会直接返回json,这就恶心,明明要接受blob,又给我个json,
所以就需要进行判断然后转换blob,然后提醒:

正常情况下:
在这里插入图片描述
非正常情况:
在这里插入图片描述

我们需要接收到后转成json:

 //转换步骤
      const file = new FileReader();
       file.readAsText(res.data, 'utf-8');
       file.onload = function () {
           const obj = JSON.parse(file.result);
           console.log(obj, 'obj')
           if (obj.code === '10000') {
               message.error(obj.message || '下载失败!')
           }
           return obj;//此时message为转化好的json格式
       }

在这里插入图片描述
file.onload方法会自动执行,所以直接在这个里面增加提醒就行。嗯,就这样。

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

六卿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值