Search code examples
angularjscordovaionic-frameworkngcordova

Check for the existence of file in cordova


I'm attempting to check for the existence of a file with cordova in ionic but I keep getting a NOT_FOUND_ERR message. If I change nativeURL to fullPath I get a ENCODING_ERR. The official documentation asks for two parameters: path and file. Some examples online show the file name/path as the only parameter. I've tried both versions with no luck.

        myFactory.transfer_file(url, download, {}, true, function(state, ob){
            if(state=="success"){
              $cordovaFile.checkFile(ob["nativeURL"]).then(function(result) {
                  console.log('File Success!');
              }, function(err) {                 
                  console.log(JSON.stringify(err));
              });

            }
        });

The file_transfer function is just a wrapper I've created around $cordovaFileTransfer.download

  $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
  .then(function(result) {
    callback("success", result);
  }, function(err) {

    callback("fail", err);

  }, function (progress) {
    callback("progress", progress);
  });

The transfer works correctly and the file now exists on the file system which I've checked using adb. But it's not found using the checkFile function.

I'm not sure what the problem is considering that I'm using the exact path that's being returned on completion of the file transfer.


Solution

  • Use the two parameter version thats in the current documentation. The first path parameter should only be the basepath. The second should be the remaining path and the filename.

    So if you used cordova.file.dataDirectory+"images/img1.png" to create the file, the code would be $cordovaFile.checkFile(cordova.file.dataDirectory, "images/img1.png") to check if it exists.

    Not $cordovaFile.checkFile(cordova.file.dataDirectory+"images/", "img1.png") as I first tried.