I am trying to save sample file to a dataDirectory but keep getting
FileError {code: 5} which is if i am right to use this document to see what is wrong
https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md
5 ENCODING_ERR
if as filePath i just pass log2.txt it file is sucessfully written
also i have tried filePath =$window.cordova.file.dataDirectory+‘log2.txt’ but i get the same error
$window.cordova.file.dataDirectory filePath is right and points to directory to which it should point
im using telerik app builder to test it and in my case it is
C:/Users/VjerN/Documents/Telerik/Icenium/Simulator/Storage/Temporary/dataDirectory
on physical device it is
file:///data/data/com.coolappz.canelio/files/
So any help what to do?
var blob = new Blob(["sample text"], { type: 'text/plain' });
$cordovaFile.writeFile($window.cordova.file.dataDirectory+'/log2.txt', blob, { 'append': false }).then(function (result) {
// Success!
console.log("writing sucess");
}, function (err) {
console.log(err);
console.log("writing error");
// An error occurred. Show a message to the user
});
i have also tried this
which also aint working
as native url i get
file:///%simulator_persistent_root%/dataDirectory/
and on physical device it is
file:///data/data/com.coolappz.canelio/files/
window.resolveLocalFileSystemURL($window.cordova.file.dataDirectory, function (dir) {
console.log(dir.nativeURL);
$cordovaFile.writeFile(dir.nativeURL+'log2.json', blob, { 'append': false }).then(function (result) {
// Success!
console.log("writing sucess");
$cordovaFile.readAsText('log2.json').then(function (sucess) {
console.log("sucess reading file");
console.log(sucess);
}, function (error) {
console.log(error);
});
}, function (err) {
console.log(err);
console.log("writing error");
// An error occurred. Show a message to the user
});
});
I faced the same problem here but I get it working with the following code:
function writeFile(fileName, data, options) {
var deferred = $q.defer();
$window.resolveLocalFileSystemURL($window.cordova.file.dataDirectory,
function(dir) {
dir.getFile(fileName, {
create: true
}, function(fileEntry) {
fileEntry.createWriter(
function(fileWriter) {
if (options['append'] === true) {
// Start write position at EOF.
fileWriter.seek(fileWriter.length);
}
fileWriter.onwriteend = function(evt) {
evt.fileEntry = fileEntry;
deferred.resolve(evt);
};
fileWriter.write(data);
},
function(error) {
deferred.reject(error);
}
);
}, function(er) {
deferred.reject(error);
});
});
return deferred.promise;
}
Hope it helps!