nodejs base64 to blob conversion

11,557

Solution 1

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")
            }
        });

Solution 2

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");
Share:
11,557

Related videos on Youtube

Rajat Barman
Author by

Rajat Barman

Updated on July 29, 2022

Comments

  • Rajat Barman
    Rajat Barman almost 2 years

    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.

    • KALALEX
      KALALEX almost 6 years
      Maybe use atob() ? Source