Javascript 如何将图像数据响应转换为图像base64?
这是我的api 当我在《邮递员》中测试这一点时,我得到了一张图片。但我如何使用Javascript 如何将图像数据响应转换为图像base64?,javascript,reactjs,axios,Javascript,Reactjs,Axios,这是我的api 当我在《邮递员》中测试这一点时,我得到了一张图片。但我如何使用axios。 我得到了非常长的字符串(如�PNG…)现在使用Axios 那么我如何使用这个响应来显示图像 axios.get(RequestURL, { withCredentials: false }) .then(function (response) { // handle success co
axios
。
我得到了非常长的字符串(如�PNG…)现在使用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时也会出现错误
DomeException:未能在“窗口”上执行“btoa”:要删除的字符串
编码包含拉丁1范围以外的字符
HTML
这不是base64图像
https://static.xx.fbcdn.net/rsrc.php/v3/yN/r/EWLVhDVJTum.png
这是png图像是的,不是,但我想将其转换为base64,以便在图像上显示。无需转换为base64,那么为什么不将此问题作为副本关闭,而不是复制/粘贴另一个答案中的代码?我缺少什么吗?URL.createObjectURL不是函数请使用window.URL.createObjectURL
我可以在本地保存此图像吗??as base64
<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
})