I have an app written in javascript that lets a user save an image to their disk. This involves node.js. If I ask to overwrite a previously saved image, I get mixed results when it comes to getting a warning from the OS that I am about to overwrite the file. If I attempt to overwrite the image by selecting the old image from the save file dialog, it prompts a warning. If I overwrite an image by simple typing in the same filename, it overwrites the image but does not give any warning.
The node.js function:
var fs = require('fs');
exports.buildFile = function(name, value) {
if (name.search(/\.[a-z]+/) == -1) {
name = name + ".png";
}
var img = new Buffer(value, encoding='base64');
fs.writeFile(name, img, function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
};
Just checks to see if the user specified a file type, and if not, appends .png and writes to disk.
Front end:
var global_imageToSave = null;
function screen_shot(fn) {
global_currModel.menuShouldHide = true;
global_currModel.setTextDeletables(false,true);
html2canvas($('body'), {
onrendered: function(canvas) {
global_imageToSave = canvas.toDataURL("image/png").split(',')[1];
global_currModel.menuShouldHide = false;
global_currModel.draw();
sc.buildFile(fn, global_imageToSave);
}
});
}
Uses html2canvas to take a screen shot and then passes that data and the file name recieved from the html dialog.
HTML Dialog onchange event:
$('[id^="model_sales_asset"]').live("pagecreate", function() {
$(this).find('.fileDialog').change(function() {screen_shot($('.fileDialog').val());});
});
As you can see the filetype is being appended before any call to the OS to save the file so I can't figure out why it matches filenames only when it is entered automatically by selecting a file.
This behavior is consistent on windows 7 and 8 as well as mac os 10.7.
The problem is not the name, it is the file I believe. The encodings are different between html2canvas and the normal file upload. I think OS is detecting a revision of a file in one case, and a completely different file in another case.
Best suggestion is to do the detection on your own, which is the proper way. Check to see if the file exists then spit out a warning. Otherwise you will get different results with different systems and plugins.