【问题标题】:How can i read a file from iCloud in an iOS-device with cordova?如何在带有 cordova 的 iOS 设备中从 iCloud 读取文件?
【发布时间】:2016-07-05 08:06:26
【问题描述】:

我想阅读存储在 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);

这就是输出:

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

我做错了什么?

【问题讨论】:

    标签: ios angularjs cordova filepicker cordova-plugin-file


    【解决方案1】:

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

    据此,我更改了“fileDir”,以便将 cordova.file 属性映射与真实设备上的物理路径匹配,这在 iOS 文件系统布局 cordova-plugin-file 的文档。

    现在可以了:)

    这是最终代码:

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

    【讨论】:

      猜你喜欢
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      • 2016-06-17
      • 2014-01-13
      • 2017-08-13
      • 1970-01-01
      相关资源
      最近更新 更多