I am using node.js, trying to save a file, no errors are thrown, yes the image won't save. This is how I am saving the file:
var url = 'captures/' + getFileName() + '.png';
fs.writeFile(url, base64, 'base64', function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
With a helper to make the file names for me:
function getFileName(){
var d = new Date()
return d.getMonth()+'-'+d.getDate()+'-'+d.getYear()+'-'+d.getHours()+'-'+d.getMinutes()+d.getSeconds();
}
Anyone had trouble with this?
fs. writeFileSync and fs. writeFile both overwrite the file by default. Therefore, we don't have to add any extra checks.
The only difference between writeFile and writeFileSync is in catching and handling the errors; otherwise, all parameters mentioned are available in both functions.
The fs. writeFileSync() is a synchronous method. The fs. writeFileSync() creates a new file if the specified file does not exist.
writeFile() is an asynchronous method for writing data in files of any type.
the problem is because this call is async and probably is loosing the context right after, I was able to fix it on my end by using fs.writeFileSync
which does it synchronously. hope this helps
Add a console.log('captures/' + getFileName())
just to make sure your file name is correct. When I had this problem it turned out that I had a problem with the file path/name and node just wasn't throwing me an error to explain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With