检查cordova中文件是否存在

Check for the existence of file in cordova

我正在尝试使用 ionic 中的 cordova 检查文件是否存在,但我不断收到 NOT_FOUND_ERR 消息。如果我将 nativeURL 更改为 fullPath,我会得到 ENCODING_ERR。官方文档要求两个参数:路径和文件。一些在线示例将文件 name/path 显示为唯一参数。我试过两个版本都没有运气。

        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));
              });

            }
        });

file_transfer 函数只是我围绕 $cordovaFileTransfer.download

创建的包装器
  $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
  .then(function(result) {
    callback("success", result);
  }, function(err) {

    callback("fail", err);

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

传输工作正常,文件现在存在于我使用 adb 检查过的文件系统中。但是使用checkFile函数没有找到。

考虑到我使用的是文件传输完成时返回的确切路径,我不确定问题出在哪里。

使用 current documentation 中的两个参数版本。第一个路径参数应该 only 是基本路径。第二个应该是剩余的路径和文件名。

因此,如果您使用 cordova.file.dataDirectory+"images/img1.png" 创建文件,代码将是 $cordovaFile.checkFile(cordova.file.dataDirectory, "images/img1.png") 以检查它是否存在。

不像我第一次尝试的那样 $cordovaFile.checkFile(cordova.file.dataDirectory+"images/", "img1.png")