How to convert base64 image to file?

Hi Everyone,

I have an image in base64 format, I want to store it on the server. I have used this code for converting it to file, the file gets generated but I can not open image.

**client.js**
**  Meteor.call('upload_image', base64data,function(error, result){
                    if(error){
                        alert('Error');
                    }
                    else{
                    alert('Done');
                        }

                    }); 
**
**main.js (server)**
Meteor.methods({

	Images.write(new Buffer(base64Data, 'base64'), {
      fileName: 'sample.png',
      type: 'image/png'
    }, function (error, fileRef) {
      if (error) {
        throw error;
      } else {

      		alert('uploaded');
      }
    });
});

Can anyone suggest me a way, so that I can display this image to my user?

You should try this.

EDIT: And store the images as a base64 string. BUT if you are thinking about storing a lot of images this way, you should consider something like AWS S3. If its only for an avatar its okay, I guess.

1 Like

Thanks @jhuenges, I have solved this by saving file that created by converting blob to file.

Please show your working code.

Also, did you use a fs library to do this?