javascript - How do I read and write a file on Phonegap? -
i looking through filewriter , filereader apis cordova , understand asynchronous.
i've managed filewriter , filereader work separately following full examples here.
but wondering if there way read file after writing it. code below shows want in gotfilewriter
function ondeviceready() { window.requestfilesystem(localfilesystem.persistent, 0, gotfs, fail); } function gotfs(filesystem) { filesystem.root.getfile("readme.txt", {create: true, exclusive: false}, gotfileentry, fail); } function gotfileentry(fileentry) { fileentry.createwriter(gotfilewriter, fail); } function gotfilewriter(writer) { writer.onwriteend = function(evt) { // read file after writing }; writer.write("some sample text"); } function fail(error) { console.log(error.code); }
filereader in full example documentation requires file
object read (that gotfilewriter
method lacks reference to). however, of asynchronous process reading files similar writing files.
if wanted read file after writing it, have start entire asynchronous process again call window.requestfilesystem(localfilesystem.persistent, 0, gotfs, fail);
in onwriteend
function? along different gotfileentry
method calls fileentry.file()
? or there way file
object within gotfilewriter
method without having repeat these steps?
does know fastest way of doing it?
use function on starting of application. read , write files. try it.
function ondeviceready() { window.requestfilesystem(localfilesystem.persistent, 0, onfilesystemsuccess, fail); window.resolvelocalfilesystemuri("file:///example.txt", onresolvesuccess, fail); var isapp = 'yes'; var root = this; cb = window.plugins.childbrowser; call(); } function onfilesystemsuccess(filesystem) { console.log(filesystem.name); } function onresolvesuccess(fileentry) { console.log(fileentry.name); } function fail(evt) { console.log(evt.target.error.code); } function call(){ window.requestfilesystem(localfilesystem.persistent, 0, successdirectoryreader, null); } function successdirectoryreader(filesystem) { try { var direntry = filesystem.root; var directoryreader = direntry.createreader(); directoryreader.readentries(success,failure); } catch (e) { alert(e); } }
Comments
Post a Comment