Skip to content

Instantly share code, notes, and snippets.

@donfrancisco
Forked from alunny/file-api-sample.js
Created May 31, 2012 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donfrancisco/2846883 to your computer and use it in GitHub Desktop.
Save donfrancisco/2846883 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