Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs base64 to blob conversion

Iam capturing webcam screenshot in reactjs(react-webcam). Screenshot is in the form of base64 encoded string. I am sending the base 64string to nodejs and I want to convert base64 String to .jpeg file, So that I can save in Azure Blob Storage. Is there any method to convert base64 string to .jpeg file.

like image 581
Rajat Barman Avatar asked Nov 08 '22 06:11

Rajat Barman


2 Answers

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");
like image 169
Ahsan Jamal Avatar answered Nov 14 '22 20:11

Ahsan Jamal


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")
            }
        });
like image 37
Rajat Barman Avatar answered Nov 14 '22 22:11

Rajat Barman