繁体   English   中英

如何将图像数据响应转换为图像 base64?

[英]How to convert image data response to Image base64?

这是我的 api

https://besticon-demo.herokuapp.com/icon?url=facebook.com&size=80..120..200

当我在 postman 中测试这个时,我得到了 Image..但是我该如何处理axios 我现在用 Axios 得到很长的字符串(比如...)

那么我如何使用该响应来显示图像..

   axios.get(RequestURL, { withCredentials: false })
                .then(function (response) {
                    // handle success
                    console.log(response)
                    var b64Response = btoa(response.data) 
                    setImageServer('data:image/png;base64,' + b64Response) // not showing image
                })

尝试运行btoa时也会出错

DOMException:无法在“Window”上执行“btoa”:要编码的字符串包含 Latin1 范围之外的字符。

HTML

 <img src={ImageServer}/>
axios.get('RequestURL', { responseType: 'arraybuffer' })
.then(response => {
      let blob = new Blob(
        [response.data], 
        { type: response.headers['content-type'] }
      )
      let image = window.URL.createObjectURL(blob)
      return image
    })
axios.get('RequestURL', {responseType: 'blob'})
.then(response => {
  let imageNode = document.getElementById('image');
  let imgUrl = URL.createObjectURL(response.data)
  imageNode.src = imgUrl
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM