javascript - Writing files in Cordova/ionic doesn't work -
i'm writing app using cordova/ionic , i'm trying write files filesystem using $cordovafile plugin. try code docs (with added logging):
$cordovafile.writefile(cordova.file.datadirectory, "file.txt", "the text inside file", true) .then(function (success) { console.log('success: ' + json.stringify(success)); }, function (error) { console.log('error: ' + json.stringify(error)); });
but returns error: {"code":5}
in 5
refers encoding_err
.
so after trying different combinations, changing first line (so without dir):
$cordovafile.writefile("file14.txt", "text", true)
returns (formatted readability):
success: { "type": "writeend", "bubbles": false, "cancelbubble": false, "cancelable": false, "lengthcomputable": false, "loaded": 0, "total": 0, "target": "filename": "", "length": 4, "localurl": "cdvfile://localhost/persistent/file14.txt", "position": 4, "readystate": 2, "result": null, "error": null, "onwritestart": null, "onprogress": null, "onwrite": null, "onabort": null, "onerror": null }
so try reading out same file using:
$cordovafile.readastext("file14.txt") .then(function (success) { console.log('success: ' + json.stringify(success)); }, function (error) { console.log('error: ' + json.stringify(error)); });
which surprise returns empty string: success: ""
so i'm wondering:
- why copy-pasting example code lead
5 encoding_err
? - why work when eliminate dir?
- how can read out file i've created?
this tutorial easy follow , works on intel xdk uses cordova , work long have cordova ( has nothing intel xdk api ) , make sure cordova.file
object defined in code .
please note there dramatic change in cordova file system starting ver 1.2 , please note current ver 1.3.3 have bugs regarding loading files current workspace ( www folder ) , writing , read files , application storage folder or internal storage dont have issues.
document.addeventlistener("deviceready", ondeviceready, false); function ondeviceready() { requestfilesystem(localfilesystem.persistent, 0, onsuccess, onerror); } function onsuccess(filesystem) { var directoryentry = filesystem.root; //lets create file named readme.txt. getfile method creates file , returns pointer(fileentry) if doesn't exist otherwise returns pointer it. returns file pointer callback parameter. directoryentry.getfile("readme.txt", {create: true, exclusive: false}, function(fileentry){ //lets write file fileentry.createwriter(function(writer){ writer.write("this text inside readme file"); }, function(error){ console.log("error occurred while writing file. error code is: " + error.code); }); }, function(error){ console.log("error occurred while getting pointer file. error code is: " + error.code); }); } function onerror(evt) { console.log("error occurred during request file system pointer. error code is: " + evt.code); }
Comments
Post a Comment