i am using node.js to save canvas to image
img in writeFile is extractedby using toDataURL on my canvas element.
it doenot save file
here is my code
var fs = IMPORTS.require('fs');
var path = IMPORTS.require('path');
path.exists('images/', function(exists){
if (exists) {
fs.writeFile('images/icon.png', img, function(err){
if (err)
callback({
error: false,
reply: err
});
console.log('Resized and saved in');
callback({
error: false,
reply: 'success.'
});
});
}
else {
callback({
error: true,
reply: 'File did not exist.'
});
}
});
Here is a literal example of how to save canvas data to a file in Nodejs. The variable
imgis a string generated bycanvas.toDataURL(). I’ve assumed you already know how to POST that string from the browser to your Nodejs server.HTML snippet that generates the sample image I used:
Nodejs snippet to decode the base64 data and save the image:
Output: