• uni-app 实现后端返回的图片文件流转base64


    使用uni-app开发webapp,由于后端返回的图片文件是二进制文件流形式,前端展示需要转换格式,首先想到的就是转成base64进行展示,话不多说,直接填坑。

    使用uni-app的uni.request进行网络请求,在网上查找的资料是如果返回的是文件流需要设置responseType为‘bold’类型,uni-app官网文档描述:设置响应的数据类型。合法值:text、arraybuffer。

    故需要设置responseType为arraybuffer类型。

    return request({
            method: "GET", // 请求方式
              url: platFormUrl+'/downFile.do', // 请求url
               responseType:'arraybuffer',
               data: data // 参数
            })

    请求拿到数据之后转换成blod对象,blod对象转base64

    let blob = new Blob([res],{type: 'image/png'})

      this.blobToDataURL(blob,(res)=>{
        console.log(res)
      })

    blod对象转base64方法

    blobToDataURL(blob, callback) {
       let a = new FileReader();
       a.onload = function(e) {
          callback(e.target.result);
       }
       a.readAsDataURL(blob);
    }
  • 相关阅读:
    洛谷P1330 封锁阳光大学
    洛谷P1341 无序字母对
    Bzoj1059 [ZJOI2007]矩阵游戏
    POJ2337 Catenyms
    Bzoj2342 [Shoi2011]双倍回文
    Bzoj1009 [HNOI2008]GT考试
    Bzoj3670 [Noi2014]动物园
    POJ2406 Power Strings
    POJ 2752 Seek the Name, Seek the Fame
    POJ3522 Slim Span
  • 原文地址:https://www.cnblogs.com/zhanglongke/p/14296447.html
Copyright © 2020-2023  润新知