cordova-plugin-file
cordova-plugin-file copied to clipboard
All files reads gave a error code 5; ENCODING_ERR in latest release on Android
How to replicate the issue
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
fs.root.getFile(cordova.file.applicationDirectory + "www/index.html", { }, function (fileEntry) {
//DO NOTHING
}, function(err) {
console.log("error accesing the file", err);
});
}, function error(err) {
console.log("error requesting the filesystem", err);
});
This outputs:
error accessing the file FileError {code: 5}
The file exists, it is a Cordova project, and it also shows by doing:
function listDir(path){
window.resolveLocalFileSystemURL(path,
function (fileSystem) {
var reader = fileSystem.createReader();
reader.readEntries(
function (entries) {
console.log(entries);
},
function (err) {
console.log(err);
}
);
}, function (err) {
console.log(err);
}
);
}
listDir(cordova.file.applicationDirectory + "www/index.html");
This shows that the file is present.
I'm getting the same issue, except using Ionic. It works perfectly on iOS. cordova-plugin-file-opener2 2.1.4
and using com.android.support:support-v13:26.+
(not v4 due to a plugin conflict).
Opening the file:
this.fileOpener.open('file:///android_asset/www/assets/pdf/mypdf.pdf', 'application/pdf')
.then(() => console.log('File is opened'))
.catch(e => console.log('Error opening file', e));
// Error opening file {status: 9, message: "File not found"}
Checking that the file exists:
this.file.checkFile(path, filename)
.then(result => console.log('File exists ', result))
.catch(e => console.log('Problem checking file ', e));
// Problem checking file {code: 5, message: "ENCODING_ERR"}
Ionic info:
Ionic:
ionic (Ionic CLI) : 4.1.2
Ionic Framework : ionic-angular 3.9.2
@ionic/app-scripts : 3.2.0
Cordova:
cordova (Cordova CLI) : 8.1.2 ([email protected])
Cordova Platforms : android 7.1.4, ios 4.5.5
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 1.2.1, (and 15 other plugins)
System:
Android SDK Tools : 26.1.1 (~/Library/Android/sdk)
ios-deploy : 2.0.0
NodeJS : v8.12.0 (~/.nvm/versions/node/v8.12.0/bin/node)
npm : 6.4.1
OS : macOS
Xcode : Xcode 10.1 Build version 10B61
Any solutions to this yet?
@domingosl @alex-steinberg @RonCan Try this:
const justTheNameOfTheFile = "file.txt";
window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, async fs => {
window.resolveLocalFileSystemURL(window.cordova.file.dataDirectory, async dirEntry => {
dirEntry.getFile(justTheNameOfTheFile, { create: true, exclusive: false }, fileEntry => {
// what you want to do
}
});
});
It is because window.cordova.file.dataDirectory
returns file:///data/user/0/...
and .getFile
checks if there is a :
in the string and throws the encoding error then. But with the solution above it worked for me.
What version of this plugin and version of Android does this affect?
I know github here is not for help, but can someone help here? https://stackoverflow.com/questions/56674724/apache-cordova-resolvelocalfilesystemurl-function-always-return-error-5-encod It's the same issue
8 months later... always the same issue...
hello this is 2020, works with ios sim, but not real device
FYI I'm migrating my app to Capacitor, really enjoying it so far. It has removed all the headaches associated with Cordova. Most native functionality you'd need is native to Capacitor (like file system). Highly recommended.
FYI I'm migrating my app to Capacitor, really enjoying it so far. It has removed all the headaches associated with Cordova. Most native functionality you'd need is native to Capacitor (like file system). Highly recommended.
Yeah...after Adobe running away, Cordova does not cut it anymore
Is Cordova dead? Seriously, their sqlite plugin is broken, I've been trying to write to a file all day, and nothing I try works. Not impressed.
Is Cordova dead? Seriously, their sqlite plugin is broken, I've been trying to write to a file all day, and nothing I try works. Not impressed.
Give Capacitor a try
@domingosl @alex-steinberg @RonCan Try this:
const justTheNameOfTheFile = "file.txt"; window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, async fs => { window.resolveLocalFileSystemURL(window.cordova.file.dataDirectory, async dirEntry => { dirEntry.getFile(justTheNameOfTheFile, { create: true, exclusive: false }, fileEntry => { // what you want to do } }); });
It is because
window.cordova.file.dataDirectory
returnsfile:///data/user/0/...
and.getFile
checks if there is a:
in the string and throws the encoding error then. But with the solution above it worked for me.
Hi guys i am having a error anybody how to fix ? thanks Asyc function not working, only work with this but i have a different error , i check permission but the problem apears is in android >10
window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, function() { window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory+"Pictures", function(dirEntry) { console.log(dirEntry); dirEntry.getFile("IMG_20210924_102017.jpeg", { create: true, exclusive: false }, function(fileEntry) { console.log(fileEntry); fileEntry.remove(function (file) {
console.log("file removed!");
}, function (error) {
console.log("error occurred: " + error.code);
}, function () {
console.log("file does not exist");
});
})
});
});
But i have error 6
NO_MODIFICATION_ALLOWED_ERR: 6
error occurred: 6
@domingosl @alex-steinberg @RonCan Try this:
const justTheNameOfTheFile = "file.txt"; window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, async fs => { window.resolveLocalFileSystemURL(window.cordova.file.dataDirectory, async dirEntry => { dirEntry.getFile(justTheNameOfTheFile, { create: true, exclusive: false }, fileEntry => { // what you want to do } }); });
It is because
window.cordova.file.dataDirectory
returnsfile:///data/user/0/...
and.getFile
checks if there is a:
in the string and throws the encoding error then. But with the solution above it worked for me.
What is the use of "fs" in second line? Do we really need it?
getFile
expects a filename not a file path.
It's intended (for either reading or creating) to get the inside the DirectoryEntry
.
Please see the examples for valid usages.