Morning
To create a file on the local file system, I use the following. This creates a file if it does not already exist.
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("test.txt", {create: true}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
alert("write success");
};
writer.write("We are testing")
}
function fail(error) {
if(error.code == 1){
alert('not found');
}
alert(error.code);
}
However, I need to write to the file ONLY if it does not already exist. I tried to use
function gotFS(fileSystem) {
fileSystem.root.getFile("test.txt", null, gotFileEntry, fail);
}
function gotFS2(fileSystem) {
alert('trying again');
fileSystem.root.getFile("test.txt", {create:true}, gotFileEntry, fail);
}
function fail(error) {
if(error.code == 1){
alert('not found');
gotFS2(fileSystem);
}
alert(error.code);
}
and then calling gotFS2 if error.code == 1 but it didn’t do anything - it didn’t even create a file when it did not exist.
It seems that gotFS2 has not been called, but alert('not found');in the fail function (error) it works.
What is the easiest way to do what I'm trying to do?
source
share