图片流 渲染到前端页面

遇到这样的一个问题

接口返回的是一个这样的图片流格式

image

打印后得到的是

image

前端如何处理呢?

1. axios 请求封装时 ,需要加上


responseType': 'arraybuffer'

具体点呢?


axios.get(url, {

... 此处省略一些参数

responseType': 'arraybuffer

    })

这时候请求接口,怎么读取数据呢?

打印后得到是这这样一串内容。

image


 //转  ArrayBuffer 成 base64

    transformArrayBufferToBase64 (buffer) {

      let binary = '';

      let bytes = new Uint8Array(buffer);

      for (var len = bytes.byteLength, i = 0; i < len; i++) {

        binary += String.fromCharCode(bytes[i]);

      }

      return window.btoa(binary);

    },

//获取验证码

    getDxcodeImg () {

      api.getValidateImage().then(res => {

        let temp = this.transformArrayBufferToBase64(res) // 将ArrayBuffer 转成base64

        this.dxcodeImg = `data:image/png;base64,${temp}` //这个数据就可以渲染到img标签中

      })

    },

评论

暂无评论