Skip to content

Instantly share code, notes, and snippets.

@alunny
Created February 25, 2012 00:57
  • Star 16 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
Star You must be signed in to star a gist
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>
@rajgurung
Copy link

splendid

@sunnyprakash
Copy link

can anyone share phonegap.js javascript file for this program?
Thanx in advance

@sunnyprakash
Copy link

can anyone share phonegap.js javascript file for this program? I need the same for WebOS.
Thanx in advance

@marcanthonyphoto
Copy link

I have a problem. The script works great but how can I add a button to delete the entry from the database?

@Viswajeet
Copy link

Its working properly but I need to know that where does the file is saved in the device.Please, can anyone suggest me the same as early as possible. Thank you in Advance.

@Viswajeet
Copy link

I tried this code for different platforms and got different results:
a> In android the data never gets deleted even if we uninstall the application.
b> In symbian S-60 the data gets deleted the instant the application is closed.

Did anyone faced the same problem,please tell it to me alongwith the solutions. Also I don't have the phonegap.js,is it mandatory to be used alongwith the sample given above.Please I need your help.

Thanks in advance.

@RyanOC
Copy link

RyanOC commented Jul 6, 2013

Nice, simple functional example. One question, the entries are not saved across debugging sessions, is that just the way debugging works or is the data not persisted? Thanks!

@brickpop
Copy link

brickpop commented Jun 4, 2014

I just made a wrapper on top of your example to make things easier to use: https://gist.github.com/jmoraleda/78551f439578f7c132c8
Thanks for your contribution

@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