如何使用Cordova访问iOS本地文件系统中的下载文件?

时间:2016-08-26 09:13:22

标签: ios cordova ionic-framework

我正在使用cordova-file-transfer-plugin,我可以在Android上完美地下载和播放MP3文件,但在iOS上该文件不存在。

我将目录更改为documentsDirectory,但是当我在浏览器中调用它时,它无法正常工作。我正在使用链接上显示的代码示例。

我也看了this question,但没有运气。

 var targetPath;
                    if (ionic.Platform.isIOS())
                    {
                        targetPath = cordova.file.documentsDirectory + data.value.split('/').pop();
                    } else
                    {
                        targetPath = cordova.file.dataDirectory + data.value.split('/').pop();
                    }
                    $cordovaFileTransfer.download('http://admin.lalelaknowledge.com/FileUpload/Upload/0a5eabcc-231e-4831-b72f-c8980b78615c.mp3', targetPath, {}, true)
                            .then(function (result)
                            {
                                console.log(result);

                            }, function (err) {

                                console.log(err);
                            }, function (progress) {
                                $timeout(function () {
                                    console.log((progress.loaded / progress.total) * 100);
                                });
                            });

1 个答案:

答案 0 :(得分:1)

这是您的解决方案。您需要cordova cordovaFileOpener2插件才能打开此文件。希望你已经安装好了。让我知道它是否有效。



$scope.initializeDownload = function() {
  var fileName = '0a5eabcc-231e-4831-b72f-c8980b78615c.mp3'
  var storagePath = cordova.file.dataDirectory + fileName
  window.resolveLocalFileSystemURL(storagePath, function success(fileEntry) {
    fileEntry.file(function(file) {
      $scope.downloadAttachment(file.localURL, options, trustHosts)
    })
  }, function(err) {
    window.resolveLocalFileSystemURL(cordova.file.documentsDirectory,
      function success(dirEntry) {
        dirEntry.getFile(fileName.replace(/^.*[\\\/]/, ''), {
            create: true,
            exclusive: false
          },
          function(fileEntry) {
            fileEntry.file(function(file) {
              $scope.downloadAttachment(file.localURL, options, trustHosts)
            });
          }, function(error) {
            console.log(error)
          });
      }, function(error) {
        console.log(error)
      });
  });
}


$scope.downloadAttachment = function(fileURL, trustHosts, options) {
  var ft = new FileTransfer();
  ft.onprogress = function(progress) {
    // Here you can get your download status
    //var downloadStatus = (progress.loaded / parseInt(fileSize)) * 100;
    //console.log(downloadStatus)
  };
  ft.download('http://admin.lalelaknowledge.com/FileUpload/Upload/0a5eabcc-231e-4831-b72f-c8980b78615c.mp3',
    fileURL,
    function(result) {
//Second argument is the file Mime type
      $cordovaFileOpener2.open(result.nativeURL, 'audio/mpeg').then(function(result) {
        console.log("Open success")
      }, function(err) {
        console.log("Open Error")
      });
    },
    function(error) {
      ft.abort();
      console.log(error.exception || error.body)
    },
    trustHosts,
    options
  );
}