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

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.