ngCordova checkFile/checkDir error code 5 - ENCODING_ERR

I’m trying to use ngCordova’s $cordova.checkFile function to check if a file exists.

I’m currently using the cordova.file.documentsDirectory which translates to a URL like file:///Users/Steve/.. however I keep getting a error code of 5.

Error code 5 apparently means an ENCODING_ERR, so I’ve been experimenting with replace() to replace the file:/// for cdvfile:// - I’ve tried everything.

However, if I strip out the protocol altogether, so it reads /Users/Steve/.. I get an error code of 1, which means it wasn’t found.

I have no idea how to get around this.

The strange thing is that $cordovaFile.download works perfectly fine.

Any help would be greatly appreciated!

/Users/Steve… For me this is not a valid path to a device internal file. It seems to be a directory on your iMac. With cordovaFile you can check, create, write, read to device internal Filesystem.
Also you you can up and download files from or into device FileSystem.
What file or directory are you checking?

Yes it’s a path on my Mac, but that’s because I’m using the iOS Simulator. I don’t think that’s the issue though (or maybe it is). But I’ve successfully used the $cordova.download() function with the same path and there were no issues. So I ruled that possibility out, because I’m thinking ‘It can’t work for one and not the other if that path is at fault’.

In download you can download from a valid URI (http://…) into the device internal FileSystem (cdv:///… or file:///…)
checkfile or checkdir can only be pointed to a device Internal Filesystem.
So its not strange that you can use download. I bet you download a fiel from your iMac ( path /Users/Steve…) into device filesystem somewhere ( I dont know where because I dont know how you configured the download).
For me is sure File:/// and cdvfile:/// are not valid with path /Users/Steve/…
Please show me the download and the checkfile code you made

Thanks PCR. Unfortunately, I’m quite new to Ionic/Angular/Cordova so my knowledge is quite limited.

I’m struggling to understand what you mean, can you correct my code?

 $scope.checkFile = function() {
      $cordovaFile.checkDir(cordova.file.documentsDirectory).then(function(result) {
          // Success! 
          alert('dir exists');
      }, function(err) {
          // An error occured. Show a message to the user
         alert(JSON.stringify(err));
      });
    }

$scope.checkFile();

Here’s my download code, which works perfectly

$scope.DownloadFile = function(URL,Filename,dldPrg) {
  $cordovaFile
    .downloadFile(URL, cordova.file.documentsDirectory+Filename, true)
    .then(function(result) {
      // Success!
      alert('Worked!'); 
      $scope.dldPrg.splice($scope.dldPrg.indexOf(dldPrg));
    }, function(err) {
      // Error
      alert(JSON.stringify(err));

      $scope.dldPrg.splice($scope.dldPrg.indexOf(dldPrg));
    }, function (progress) {
      $scope.dldPrg.push(dldPrg);
      // constant progress updates
      
    });//End cordovaFile
  
  }//End DownloadFile

Thanks!

First look on your code says the checkfile is pointing to a directory (cordova.file.documents). If you try to check a file you use a path to a file. So maybe you have to concat a filename to the path.
I had troubles with checkfile too. In my apps I use window.resolveLocalFileSystemURI(Url, resOnSuccess, resOnError) to check a file exists. Actually I patched the checkfile code in ngCordova waiting for a checkfile that workshop from the creator of ngCordova.

Now I have problems with checkfile and readFile too. All File function return with error code 5.

Did you allready solve the problem?

I never got to the bottom of it. I had other work to do, but I’ll be revisiting this project when I have some time.

If it’s not working for you too, then maybe something has changed with Cordova or the plugins?

If you find a solution before me, please post it here. I’m eager to know how to fix it.

Before Imused a project found on Github. After your I discovered I have the same problem as you I tried that software. But, gess what? Error code 5 too.
So I expect the problem is in the plugin.
BTW I use ngCordova in my current project without problems. To check whether a file is not allready in the FileSystem I use window.resolveFilesystem I earlier mentioned. Works as should be.

Same here, code 5 on writeFile any news on this?

pcr5d
I hope this will help you out:

app.controller(‘TestCtrl’, [’$scope’, ‘$q’, ‘$cordovaFile’,
function($scope, $q, $cordovaFile) {

console.log(‘TestCtrl’);

/*
List dir test and remove all dirs and files in test to start over again the test
*/
function ClearDirectory() {
console.log(‘ClearDirectory’);
$cordovaFile.listDir(fileDir + ‘test’).then( function(entries) {
console.log('listDir: ', entries);
}, function(err) {
console.error('listDir error: ', err);
});

	$cordovaFile.removeRecursively(fileDir + 'test')
	.then( function() {

console.log(trinlDir + ’ recursively removed’);
},
function(err) {
console.log(fileDir + ’ error: ', err);
});
}

/*
Here some examples with proper filepath
*/

function testFS() {
// Download file from 'http://www.yourdomain.com/test.jpg' to test/one/test.jpg on device Filesystem
	var hostPath = 'http://www.yourdomain.com/test.jpg';
	var clientPath = fileTransferDir + 'test/one/test.jpg';
	var fileTransferOptions = {};

	$cordovaFile.downloadFile(hostPath, clientPath, true, fileTransferOptions).then (function() {
	});
// Create dir test
	$cordovaFile.createDir(fileDir + 'test/').then( function(dirEntry) {
	});
// Create dir aganin in dir test
	$cordovaFile.createDir(fileDir + 'test/one/').then( function(dirEntry) {
	});
// Create empty file test.txt in test/again/
	$cordovaFile.createFile(fileDir + 'test/one/test.txt', true).then( function(fileEntry) {
	});
// List of files in test/again
	$cordovaFile.listDir(fileDir + 'test/one/').then( function(entries) {
	console.log('list dir: ', entries);
	});
// Write some text into file 
	$cordovaFile.writeFile(fileDir + 'test/one/test.txt', 'Some text te test filewrite', '').then( function(result) {
	});
// Read text written in file
	$cordovaFile.readAsText(fileDir + 'test/one/test.txt').then( function(result) {
console.log('readAsText: ', result);
	});
}

function testQ() {
	var hostPath = 'http://www.yourdomain.com/test.jpg';
	var clientPath = fileTransferDir + 'test/one/test.jpg';
        var fileTransferOptions = {};
	$q.all([
		$cordovaFile.downloadFile(hostPath, clientPath, true, fileTransferOptions),
		$cordovaFile.createDir(fileDir + 'test/'),
		$cordovaFile.createDir(fileDir + 'test/two/'),
		$cordovaFile.createFile(fileDir + 'test/one/test.txt', true),
		$cordovaFile.listDir(fileDir + 'test/one/'),
		$cordovaFile.writeFile(fileDir + 'test/one/test.txt', 'Some text te test filewrite', ''),
		$cordovaFile.readAsText(fileDir + 'test/one/test.txt')
	]).then( function(result) {

console.log('testQ result: ', result);
});
}

/*
Here is what I am using for my Android and IOS apps
Keep attention to a couple of things:

  • Android and IOS have other directorynames for files
  • $cordovaFile functions prefixes all pathnames with root
    $cordovaFileTransfer functions needs absolute pathnames

Here I create the prefixes for File functions and FileTransfer functions for Android and IOS
*/

$ionicPlatform.ready(function() {
	if (ionic.Platform.isAndroid()) {
console.log('cordova.file.externalDataDirectory: ' + cordova.file.externalDataDirectory);
			myFsRootDirectory1 = 'file:///storage/emulated/0/'; // path for tablet
			myFsRootDirectory2 = 'file:///storage/sdcard0/'; // path for phone
			fileTransferDir = cordova.file.externalDataDirectory;
			if (fileTransferDir.indexOf(myFsRootDirectory1) === 0) {
				fileDir = fileTransferDir.replace(myFsRootDirectory1, '');
			}
			if (fileTransferDir.indexOf(myFsRootDirectory2) === 0) {
				fileDir = fileTransferDir.replace(myFsRootDirectory2, '');
			}
console.log('Android FILETRANSFERDIR: ' + fileTransferDir);
console.log('Android FILEDIR: ' + fileDir);
		}
		if (ionic.Platform.isIOS()) {
console.log('cordova.file.documentsDirectory: ' + cordova.file.documentsDirectory);
			fileTransferDir = cordova.file.documentsDirectory;
			fileDir = '';
console.log('IOS FILETRANSFERDIR: ' + fileTransferDir);
console.log('IOS FILEDIR: ' + fileDir);
		}

	if (ionic.Platform.isAndroid() || ionic.Platform.isIOS()) {
		ClearDirectory();
		testFS();

// Other functions here
}
});

}]);

Hey I think I find a solution to it.
Refer my response at http://stackoverflow.com/a/27523006/4369765

I used to be able to call this

$cordovaFile.checkDir('/').then( successFn, errorFn )
// '/' maps to cordova.file.applicationStorageDirectory + 'Library/files' on iOS

but recently, I started getting error.code==5. I checked the code and it must have changed at some point. now I need to use this:

$cordovaFile.checkDir( cordova.file.applicationStorageDirectory, 'Library/files' )
.then( successFn, errorFn )
// '/' maps to cordova.file.applicationStorageDirectory + 'Library/files' on iOS

I was getting the error code 5 because I was writing a base64 encoded image, but hadn’t removed the data:image/jpeg;base64, string from the beginning.

hey…i having some same kind of problem…!can u help…!
Here is my ques

It’s a Bug: https://github.com/driftyco/ng-cordova/issues/506