I'm writing an app using Cordova/ionic and I'm now trying to write files to the filesystem using the $cordovaFile plugin. So I try exactly the code from the docs (with some added logging):
$cordovaFile.writeFile(cordova.file.dataDirectory, "file.txt", "the text inside the file", true)
.then(function (success) {
console.log('SUCCESS: ' + JSON.stringify(success));
}, function (error) {
console.log('ERROR: ' + JSON.stringify(error));
});
but this returns ERROR: {"code":5}
in which 5
refers to an ENCODING_ERR
.
So after trying some different combinations, changing the first line to this (so without the dir):
$cordovaFile.writeFile("file14.txt", "text", true)
returns (formatted for 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 I try reading out this 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 to my surprise just returns an empty string: SUCCESS: ""
So I'm now wondering:
5 ENCODING_ERR
?This tutorial is easy to follow , it works on intel xdk but it uses cordova , so simply will work as long as you have cordova ( it has nothing to do with intel xdk api ) , just make sure the cordova.file
object is defined in your code .
Please note that there are dramatic change in cordova file system starting from ver 1.2 , also please note that current ver 1.3.3 have some bugs regarding loading files from your current workspace ( www folder ) , however writing and read files to and from application storage folder or internal storage dont have any issues.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);
}
function onSuccess(fileSystem)
{
var directoryEntry = fileSystem.root;
//lets create a file named readme.txt. getFile method actually creates a file and returns a pointer(FileEntry) if it doesn't exist otherwise just returns a pointer to it. It returns the file pointer as callback parameter.
directoryEntry.getFile("readme.txt", {create: true, exclusive: false}, function(fileEntry){
//lets write something into the file
fileEntry.createWriter(function(writer){
writer.write("This is the text inside readme file");
}, function(error){
console.log("Error occurred while writing to file. Error code is: " + error.code);
});
}, function(error){
console.log("Error occurred while getting a pointer to file. Error code is: " + error.code);
});
}
function onError(evt)
{
console.log("Error occurred during request to file system pointer. Error code is: " + evt.code);
}