0

I have a variable in JavaScript name as myImage which holds the base64 encoded string of an image like data:image/png;base64,iVBORw0KGgoAAAANSUhE...... Now I want to save this image on folder at server side. Please Help

Thanks in Advance

  • 2
    Possible duplicate from http://stackoverflow.com/questions/11511511/how-to-save-a-png-image-server-side-from-a-base64-data-string –  May 17 '13 at 04:32

2 Answers2

4

You need to work on asking questions a little better, but I will try to give you a basic explanation.

Step one: Pass the base64 string to the server.

Step two: Strip the first parts and last parts.

Step three: base64_decode the string.

Step four: file_put_contents the result of that.

Of course this is very basic, but there are already answers out there for this, and I was able to find even more with a google search.

Zachrip
  • 3,242
  • 1
  • 17
  • 32
2

The below code does the trick:

Here you are just writing the data to a file and saving it in the write format

var data = image.replace(/^data:image\/\w+;base64,/, "");
    var buf = new Buffer(data, 'base64');
    fs.writeFile('image.png', buf,function(err, result) {
      if(err){console.log('error', err);}
    });

The resulting image will be stored as image.png at the current directory

PDHide
  • 18,113
  • 2
  • 31
  • 46