javascript - Accessing the file written using Cordova -
i have created file using cordova file api in worklight. wnat read file once write event done. tried writing function call readastext() in writer.onwriteend event. file not being read. "exclusive" option have this? works fine till read(), , there no error, no message.. think not reading file. once error occurred on reader.onloadend event :
read error {"type":"error","bubbles":false,"cancelbubble":false,"cancelable":false,"lengthcomputable":false,"loaded":0,"total":0,"target":{"filename":"file://c:\users\ibm_admin\cordova\filesystem\persistent\rqm\executionresult.xml","readystate":2,"result":null,"error":{"code":1},"onloadstart":null,"onprogress":null,"onload":null,"onabort":null}}
here code:
window.requestfilesystem(localfilesystem.persistent, 0, onfilesystemsuccess, fail); function onfilesystemsuccess(filesystem) { alert("created file system "); filesystem.root.getfile("executionresult.xml", { create : true, exclusive : true }, gotfileentry, fail); function gotfileentry(fileentry) { fileentry.createwriter(gotfilewriter, fail); } function gotfilewriter(writer) { writer.write(jsondata); writer.onwriteend = function(evt) { readafterwriting(); }; } function readafterwriting(){ filesystem.root.getfile("executionresult.xml", { create : false, exclusive : false }, gotfile, fail); }; } function gotfile(file) { readastext(file); } function readastext(file) { var reader = new filereader(); reader.onloadend = function(evt) { console.log("read text"); console.log(evt.target.result); }; reader.readastext(file); }
this code error occuring. file name shown 22. here line shown.. var callbackid = v.id;
window.addeventlistener("message", function(e){ _consolelog("*****pg bc***** " + e.domain + " said: " + e.data); var r = e.data; // if result returned _consolelog("typeof result=" + (typeof r) + " r=" + r); if (r.length > 0) { // if ((typeof r == "string") && (r.length > 0)) { _consolelog("result exec=<" + dumpobj(r, '', ' ', 2) + ">"); eval("var v=" + r + ";"); var callbackid = v.id; var cast = v.cast; // var temp = "+cast+"("+this.getjsonstring() + ");\n" if (cast) { // _consolelog("cast==="+"var // temp="+cast+"("+json.stringify(v.message)+");\n"); // eval("var temp="+cast+"("+json.stringify(v.message)+");\n"); // _consolelog("***** cast:"+" var temp="+cast+"("+r+");"); eval("var temp=" + cast + "(" + r + ");"); v = temp; } // _consolelog("after cast="+dumpobj(v, '', ' ', 3)); // if status ok, return value caller if (v.status === cordova.callbackstatus.ok) { // if there success callback, call // returned value if ((typeof(cordova.callbacks[callbackid]) != 'undefined') && (cordova.callbacks[callbackid] != null)) { if (cordova.callbacks[callbackid].success) { try { cordova.callbacks[callbackid].success(v.message); } catch (e) { _consolelog("error in success callback: " + callbackid + " = " + e); } // clear callback if not expecting more results if (!v.keepcallback) { delete cordova.callbacks[callbackid]; } } } return v.message; }
here code expect:
<!doctype html> <html> <head> <title>filereader example</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <script type="text/javascript" charset="utf-8"> onwlready = function() { // wait phonegap load document.addeventlistener("deviceready", ondeviceready, false); } var wlinitoptions = { onsuccess : onwlready, connectonstartup : false }; if (window.addeventlistener) { window.addeventlistener('load', function() { wl.client.init(wlinitoptions); }, false); } else if (window.attachevent) { window.attachevent('onload', function() { wl.client.init(wlinitoptions); }); } function log(txt){ var p = document.createelement('p'); document.getelementbyid("log").appendchild(p); p.appendchild(document.createtextnode(txt)); } // phonegap ready // function ondeviceready(){ log("ondeviceready"); } function readfile() { window.requestfilesystem(localfilesystem.persistent, 0, function(filesystem) { filesystem.root.getfile("executionresult.xml", { create : false, exclusive: false }, function(fileentry){ fileentry.file(function(file) { readdataurl(file); readastext(file); }, fail); }, fail); }, fail); } function writefile() { window.requestfilesystem(localfilesystem.persistent, 0, function(filesystem) { filesystem.root.getfile("executionresult.xml", {create: true, exclusive: true}, function(fileentry) { fileentry.createwriter( function(writer) { writer.onwriteend = function(evt) { readfile(); }; writer.onwritestart = function(evt){ log("onwritestart"); }; writer.onwrite = function(evt){ log("onwrite"); }; writer.onerror = function(evt){ log("onerror"); }; writer.write("jsondata"); }, fail); } , fail); }, fail); } function readdataurl(file){ var reader = new filereader(); log("readdataurl..."); reader.onloadstart = function(evt){ log("onloadstart: " + evt.target.result); }; reader.onload = function(evt){ log("onload: " + evt.target.result); }; reader.onloadend = function(evt){ log("onloadend: " + evt.target.result); }; reader.onerror = function(evt){ log("onerror: " + evt.target.result); }; reader.readasdataurl(file); } function readastext(file) { var reader = new filereader(); log("readastext..."); reader.onloadstart = function(evt){ log("onloadstart: " + evt.target.result); }; reader.onload = function(evt){ log("onload: " + evt.target.result); }; reader.onloadend = function(evt){ log("onloadend: " + evt.target.result); }; reader.onerror = function(evt){ log("onerror: " + evt.target.result); }; reader.readastext(file); } function fail(error) { log("fail: "+ evt.target.error.code); } </script> </head> <body id="content"> <button onclick="writefile();">write , read file</button> <p id="log"></p> </body> </html>
hope helps...
Comments
Post a Comment