If you are reading from a URL with node-fetch in NodeJS, you don't want to get a blob but rather a buffer. You can then easily convert to base64.
const b64 = await fetch(url)
.then((response) => response.buffer())
.then((buffer) => {
const b64 = buffer.toString('base64');
return b64;
})
.catch(console.error);
Answer from Geoffrey Bourne on Stack OverflowIf you are reading from a URL with node-fetch in NodeJS, you don't want to get a blob but rather a buffer. You can then easily convert to base64.
const b64 = await fetch(url)
.then((response) => response.buffer())
.then((buffer) => {
const b64 = buffer.toString('base64');
return b64;
})
.catch(console.error);
For NodeJS (with node-fetch), you can get a buffer from blob using Buffer.from(await blob.arrayBuffer());.
However, you don't need to get a blob from node-fetch.
You can just get a buffer directly from the response.
And the info from blob.type can be obtained from the response header.
For example, to get a data url, using blob:
async function toDataURL_node(url) {
let response = await fetch(url);
let blob = await response.blob();
let buffer = Buffer.from(await blob.arrayBuffer());
return "data:" + blob.type + ';base64,' + buffer.toString('base64');
}
And without using blob:
async function toDataURL_node(url) {
let response = await fetch(url);
let contentType = response.headers.get("Content-Type");
let buffer = await response.buffer();
return "data:" + contentType + ';base64,' + buffer.toString('base64');
}
» npm install blob-to-base64
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
var base64data = reader.result;
console.log(base64data);
}
Form the docs readAsDataURL encodes to base64
As an awaitable function:
function blobToBase64(blob) {
return new Promise((resolve, _) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
}
Note: The blob's result cannot be directly decoded as Base64 without first removing the Data-URL declaration preceding the Base64-encoded data. To retrieve only the Base64 encoded string, first remove data:/;base64, from the result.
There is a pure JavaScript way that is not depended on any stacks:
const blobToBase64 = blob => {
const reader = new FileReader();
reader.readAsDataURL(blob);
return new Promise(resolve => {
reader.onloadend = () => {
resolve(reader.result);
};
});
};
For using this helper function you should set a callback, example:
blobToBase64(blobData).then(res => {
// do what you wanna do
console.log(res); // res is base64 now
});
I write this helper function for my problem on React Native project, I wanted to download an image and then store it as a cached image:
fetch(imageAddressAsStringValue)
.then(res => res.blob())
.then(blobToBase64)
.then(finalResult => {
storeOnMyLocalDatabase(finalResult);
});
» npm install @maruware/blob-to-base64
» npm install base64-blob
You can convert your Base64 string to Buffer and then try storing it to azure.
var base64String = "....."; // your base64 string
var bufferValue = Buffer.from(base64String,"base64");
I used this and it worked. below is server side code(NodeJS)
var contentType = 'image/jpeg';
let base64String=req.body.img;
let base64Image = base64String.split(';base64,').pop();
let date=Date.now();
fs.writeFile(`./uploads/${date}.jpeg`, base64Image, {encoding: 'base64'}, function(err) {
console.log('File created');
sourceFilePath= path.resolve(`./uploads/${date}.jpeg`);
blobName=path.basename(sourceFilePath, path.extname(sourceFilePath));
//console.log(sourceFilePath);
blobService.createBlockBlobFromLocalFile(containerName, blobName, sourceFilePath, err => {
if (err) {
console.log(err);
}
else {
//resolve({ message: `Upload of '${blobName}' complete` });
console.log("UPLOADED")
}
});