I am trying to upload an image that I extract from a my canvas and post via ajax, and I have trouble creating the image file on my server side.
I have following this answer: https://mcmap.net/q/1471183/-how-to-write-binary-data-to-a-file-using-node-js but still I seem to miss something here or there.
Here is my javascript (browser) code:
var img = myCanvas.toDataURL("image/jpeg");
// ajax request to send the image
and in server side, I do the following:
var fd = fs.openSync('./img.jpeg', 'w');
req.body.image = req.body.image.replace(/^data:image\/\w+;base64,/, "");
console.log( req.body.image );
var buff = new Buffer(req.body.image, 'base64');
fs.write(fd, buff, 0, buff.length, 0, function(err,written){
console.log( ">> "+ err );
fs.closeSync( fd );
});
As you can see, I've logged the image to see if it was properly sent, and IT IS, but still I can't open the created img.jpeg file
any help or hint is very welcome.
EDIT
I've also tried the solution in here: https://mcmap.net/q/116563/-how-can-i-save-a-base64-encoded-image-to-disk but I still have the same problem
EDIT:2
I've tested my front-end side, with a PHP script in the server side, and the image produced was very good, so I would conclude that the problem is in my NodeJS code.
fs.writeFileSync('./img.jpeg', req.body.image.replace(/^data:image\/\w+;base64,/, ""), {encoding: 'base64'})
– Verulamiumreq.body.image
? Are you using the express bodyParser? How are you populating the POST data client-side? Base64 does not treat spaces as pluses, but that conversion could be getting introduced elsewhere. – VerulamiummyCanvas
and send it via ajax post query nothing special not even using jQuery, and in node JS I'm using the body parser of theconnect
extension – Hypnoanalysis