Created
April 14, 2010 18:41
-
-
Save alunny/366162 to your computer and use it in GitHub Desktop.
sample usage of the phonegap file api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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