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

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

最佳答案

这是您的解决方案。您需要 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
  );
}

关于ios - 如何使用 Cordova 访问 iOS 本地文件系统中的下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39162644/

相关文章:

java - Android 杀死 PhoneGap 应用程序

android - Ionic run error exit code 1 设备通信超时

android - 找不到与给定名称匹配的资源 : attr ‘android:keyboardNavigationCluster’

iphone - 拍摄完图像后,UIActivityIndi​​catorView没有显示在“相机”叠加 View 上

android - Sencha Touch+Phonegap+Android 应用引用错误

javascript - 如何从 ionic Controller 将值传递到 inAppBrowser 中?

css - 如果高度小于 X%,则将宽度设置为 100%

objective-c - 由于 CoreAnimation 和 CG 图像的脏内存

iphone - 从文档目录中的文件夹创建 ZIP 文件 - Objective C (ARC)

ios - 如何使用 Swift 编辑 iOS 日历应用程序中的事件日期?