如何使用Cordova在iOS设备上的iCloud中读取文件?

Mou*_*Tio 2 filepicker ios angularjs cordova cordova-plugin-file

我想阅读存储在iCloud中的PDF文件的内容。

  1. 我使用FilePicker Phonegap iOS插件(https://github.com/jcesarmobile/FilePicker-Phonegap-iOS-Plugin)选择文件。该插件为我提供了文件复制的临时路径。

  2. 我想读一下Cordova文件插件(https://github.com/apache/cordova-plugin-file),但是我做错了什么,日志总是给我一个错误。

这是代码:

$scope.successCallback = function (path) {
    var fileName = path.substr(path.lastIndexOf('/') + 1);
    var fileDir = path.substr(0,path.lastIndexOf('/') + 1)
    console.log("FilePath: " + path);

    $cordovaFile.readAsDataURL(fileDir, fileName)
      .then(function (data) {
          var index = data.indexOf("base64,");
          if(index > 0)
          {
              data = data.substr(index+7);
          }
          console.log("Data OK=" + data);
      }, function (error) {
          console.log("Error reading file: " + JSON.stringify(error));
      });                 
}
window.FilePicker.pickFile($scope.successCallback, $scope.errorCallback);
Run Code Online (Sandbox Code Playgroud)

这就是输出:

$FilePath: /private/var/mobile/Containers/Data/Application/22E33EF4-832B-4911-92A6-312927C42A7C/tmp/DocumentPickerIncoming/file.pdf
$Error reading file: {"code":5,"message":"ENCODING_ERR"}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Mou*_*Tio 5

我意识到在文件路径中是一个“ tmp”文件夹。

据此,我更改了“ fileDir”以使cordova.file属性映射到真实设备上的物理路径,这在cordova-plugin-file文档的iOS文件系统布局中进行了引用。

现在可以了:)

这是最终代码:

$scope.successCallback = function (path) {
var fileName = path.substr(path.lastIndexOf('/') + 1);
var fileDir = cordova.file.tempDirectory + "DocumentPickerIncoming/";

console.log("FilePath: " + path);

$cordovaFile.readAsDataURL(fileDir, fileName)
  .then(function (data) {
      var index = data.indexOf("base64,");
      if(index > 0)
      {
          data = data.substr(index+7);
      }
      console.log("Data OK=" + data);
      }, function (error) {
          console.log("Error reading file: " + JSON.stringify(error));
      });             
}
window.FilePicker.pickFile($scope.successCallback, $scope.errorCallback);
Run Code Online (Sandbox Code Playgroud)