Skip to content

Instantly share code, notes, and snippets.

@alunny
Created February 25, 2012 00:57
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save alunny/1904992 to your computer and use it in GitHub Desktop.
Save alunny/1904992 to your computer and use it in GitHub Desktop.
simple PhoneGap File API example
<html>
<body>
<form onsubmit="return saveText()">
<label for="name">Name</label><br>
<input id="name" /><br>
<label for="desc">Description</label><br>
<input id="desc" /><br>
<input type="submit" value="Save" />
</form>
<dl id="definitions">
</dl>
</body>
<script src="phonegap.js"></script>
<script>
var FILENAME = 'database.db',
$ = function (id) {
return document.getElementById(id);
},
failCB = function (msg) {
return function () {
alert('[FAIL] ' + msg);
}
},
file = {
writer: { available: false },
reader: { available: false }
},
dbEntries = [];
document.addEventListener('deviceready', function () {
var fail = failCB('requestFileSystem');
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}, false);
function gotFS(fs) {
var fail = failCB('getFile');
fs.root.getFile(FILENAME, {create: true, exclusive: false},
gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
var fail = failCB('createWriter');
file.entry = fileEntry;
fileEntry.createWriter(gotFileWriter, fail);
readText();
}
function gotFileWriter(fileWriter) {
file.writer.available = true;
file.writer.object = fileWriter;
}
function saveText(e) {
var name = $('name').value,
desc = $('desc').value,
fail;
dbEntries.push('<dt>' + name + '</dt><dd>' + desc + '</dd>')
$('name').value = '';
$('desc').value = '';
$('definitions').innerHTML = dbEntries.join('');
if (file.writer.available) {
file.writer.available = false;
file.writer.object.onwriteend = function (evt) {
file.writer.available = true;
file.writer.object.seek(0);
}
file.writer.object.write(dbEntries.join("\n"));
}
return false;
}
function readText() {
if (file.entry) {
file.entry.file(function (dbFile) {
var reader = new FileReader();
reader.onloadend = function (evt) {
var textArray = evt.target.result.split("\n");
dbEntries = textArray.concat(dbEntries);
$('definitions').innerHTML = dbEntries.join('');
}
reader.readAsText(dbFile);
}, failCB("FileReader"));
}
return false;
}
</script>
</html>
@techno-shilpa
Copy link

The data wrote in the file does not persist. Whenever I exit and start my application again it loads nothing on the page. As the readText() function is supposed to load the file data on the page as soon as device is ready. But blank page is showing instead.
Am I missing something? Can anyone help me out to resolve the issue. Its urgent.

@warengonzaga
Copy link

I try this stuff... all is functional but I've observed that this is not writing fil inside the device its only display the text you input... the phonegap.js... you can find it on buil.phonegap.com auto include that js file inside auto generated apk :)

@princenaman
Copy link

Not working on iOS Simulator, Please help.
Any permission or stuff i need to add other than this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment