Slippery Rock University | Dr. Deborah Whitfield | Go Browns! |
Device Storage
Local Storage API
for ( var i in localStorage) alert (localStorage.getItem(i));
Phonegaps two FileSystem API
document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady(){ var size =0; window.requestFileSystem(LocalFileSystem.PERSISTENT, size, onFSsuccess, onFSerror); }
function onFSsuccess(FS){ var Fin = FS.root.createReader(); Fin.readEntries(parseDir, onFSerror); } function onFSerror(err){ alert ("Error " + err); }
function parseDir (entries) { var dirs='Directories:<ul>'; var files='Files: <ul>'; for (var i=0, len=entries.length; i< len; i++) { if (entries[i].isDirectory) { dirs +='<li>' + entries[i].fullPath+ '</li>'; } else files +='<li>' + entries[i].fullPath+ '</li>'; } dirs +='</ul>'; files +='</ul>'; document.getElementById("fileslist").innerHTML += dirs + files; alert ("done parsing"); } }
function onFSsuccess(FS){ myRoot=FS.root; myRoot.getFile('sample.txt', {create: true, exclusive:false}, function(fileEntry) {setUP(fileEntry, null, true);}, onCreateErr); alert ("Success for FS"); }
myRoot.getFile('sample.txt', {create: true, exclusive:false}, onGetSuccess , onCreateErr); function onGetSuccess (fileEntry) { setUP(fileEntry, null, true); }
<button type="button" onclick="writeIT()">write hello</button> <button type="button" onclick="readIT()">Read file</button>
document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady(){ var size =0; window.requestFileSystem(LocalFileSystem.PERSISTENT, size, onFSsuccess, onFSerror); } function onFSsuccess(FS){ myRoot=FS.root; myRoot.getFile('sample.txt', {create: true, exclusive:false}, function(fileEntry) {setUP(fileEntry, null, true);}, onCreateErr); alert ("Success for FS"); } function getItReady(file) { writeFile(file, null); } function readIT() { //notice the ugly code for the file method. It can be beautified myFile.file(function (file) { var reader = new FileReader(); reader.onloadend = function() { document.getElementById("filedata").innerHTML = this.result; }; reader.readAsText(file); }, onReadErr); } function setUP(fileEntry, x, isAppend){ //setUP the file pointer for later use myFile=fileEntry; } function onFSerror(err){ alert ("FS Error " + err); } /*function parseData (entries) { //for finding a file in the dir exists =-1; for (var i=0, len=entries.length; i< len; i++) { if (entries[i].isFile && "sample.txt".test(entries[i].name)) { exists=i; break; } } if (exists == -1) { alert ("The file isn't created yet"); return; } else myFile=entries[exists]; alert ("Myfile is setup for use"); }*/ function onReadErr (err){ alert ("Read Error " + err); } function onWriteErr (err){ alert ("Write Error " + err); } function onCreateErr (err){ alert ("Create Error " + err); } function writeIT() { //getfile already called and set up myFile myFile.createWriter(onGetWriter, onWriteErr); } function onGetWriter(myWriter) { //myWriter=writer; str="ta-da,3\n;ta-da,4"; myWriter.seek(myWriter.length); myWriter.write(str); }