Skip to content

Instantly share code, notes, and snippets.

@alunny
Created April 14, 2010 18:41
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alunny/366162 to your computer and use it in GitHub Desktop.
Save alunny/366162 to your computer and use it in GitHub Desktop.
sample usage of the phonegap file api
var writeData = function (filename, data, callback) {
var writeOutData = function () {
var fw = new FileWriter();
fw.oncomplete = callback;
fw.onerror = function () {
alert("Failed to save update");
}
fw.writeAsText("myDir/" + filename, data);
}
navigator.fileMgr.testDirectoryExists("myDir", function(exists) {
exists ? writeOutData() :
navigator.fileMgr.createDirectory("myDir", writeOutData, function() {
alert("Failed to save update");
})
});
};
var readData = function (filename, callback) {
var filePath = "myDir/" + filename;
var readInData = function () {
var fr = new FileReader();
fr.onload = function (data) {
callback(data);
};
fr.readAsText(filePath);
}
navigator.fileMgr.testFileExists(filePath, function(exists) {
exists ? readInData() : alert("file doesn't exist");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment