这是我的 api
https://besticon-demo.herokuapp.com/icon?url=facebook.com&size=80..120..200
当我在 postman 中测试这个时,我得到了图像..但是我怎么能用 axios
.
我现在正在使用 Axios 获得很长的字符串(如�PNG...)
那么我如何使用该响应来显示图像..
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: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
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
})
关于javascript - 如何将图像数据响应转换为 Image base64?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67389889/