英文:
How to convert Image from Url to base64 in react js
问题
我从Firebase存储获取图像的URL,并希望将这些图像转换为Base64。
英文:
I am getting Url of images from firebase storage and want to convert these images to base64
答案1
得分: 0
尝试一下:
英文:
Give it a go:
var request = require('request').defaults({ encoding: null });
request.get(/* URL FIREBASE */, function (error, response, body) {
if (!error && response.statusCode == 200) {
data = Buffer.from(body).toString('base64');
console.log(data);
}
});
答案2
得分: 0
客户端 JavaScript 代码。使用FileReader和fetch。
const base64 = await fetch('https://i.imgur.com/gBi0yjr.png')
.then(response => response.blob())
.then(blob => {
const reader = new FileReader();
reader.readAsDataURL(blob);
return new Promise((res) => {
reader.onloadend = () => {
res(reader.result);
}
})
})
英文:
Client side js. With FileReader and fetch.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const base64 = await fetch('https://i.imgur.com/gBi0yjr.png')
.then(response => response.blob())
.then(blob => {
const reader = new FileReader();
reader.readAsDataURL(blob);
return new Promise((res) => {
reader.onloadend = () => {
res(reader.result);
}})
})
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论