我有一些图像将在React应用程序中显示。我向服务器执行GET请求,该请求返回BLOB格式的图像。然后我将这些图像转换为base64。最后,我将这些base64字符串设置为图像标记的src属性。
最近我开始使用Fetch API。我想知道是否有一种方法可以'一次性'进行转换。以下是一个示例来解释我的想法以及/或者使用Fetch API是否可能实现。我还没有在网上找到任何相关信息。
let reader = new window.FileReader();
fetch('http://localhost:3000/whatever')
.then(response => response.blob())
.then(myBlob => reader.readAsDataURL(myBlob))
.then(myBase64 => {
imagesString = myBase64
}).catch(error => {
//Lalala
})