当前位置:   article > 正文

uniapp H5项目 获取接口的二进制流转化成图片url(base64)_移动端 二进制流数据转 url

移动端 二进制流数据转 url

如果你使用的是uniapp,
并且你从接口获取下来的数据长这样:

在这里插入图片描述
想要把取到的数据展示成图片,那么你可以这样做:

    // 这是我们的项目封装的请求方法
    const res = await this.$api.getKaptcha({
       originResponse: true, // 这样写是为了在request那边特殊处理,目的是为了获取header里的数据 如果你不需要可以不写
       responseType: 'arraybuffer', // 这里一定要写 这样才能把数据转成arrayBuffer格式的
       params: { type: '1' } // 这里是我们的接口需要的参数
     })
     const { header, data } = res 
     this.key= header.captchakey // 验证码key  // 这里是我们项目这里需要从接口获取的额外数据
     this.codeUrl = 'data:image/jpeg;base64,' + uni.arrayBufferToBase64(data) // 获取到url


    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
// 图片展示
	<uv-image
      :src="codeUrl"
       width="100"
       height="32"
    ></uv-image>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/487728
推荐阅读
相关标签
  

闽ICP备14008679号