ngCordova插件zip deos不适用于Android

时间:2016-01-07 16:46:52

标签: android angularjs cordova ionic

我花了一整天的时间来解决这个问题。 文件被下载并存储在设备存储器中,而不是解压缩。

// Url of file for download
  var url = "http://exapmle/someUrl";

  // File names only
  var file = "aaa.zip";

  $scope.contenutoDownload = function () {      
    // Save location
    var targetPath = cordova.file.dataDirectory  + file;

    // Use cordovaFileTransfer to store the file in the device storage
    $cordovaFileTransfer.download(url + file, targetPath, {}, true).then(function (result) {

        console.log('File downloaded. ' + JSON.stringify(result));

        $cordovaZip
          .unzip(
            result.nativeURL,
            cordova.file.dataDirectory 
          ).then(function () {

            console.log('cordovaZip success');
            //Delete zip file
            if(cordova.platformId == 'ios') {
              $cordovaFile.removeFile(cordova.file.tempDirectory,file);
            } else {
              $cordovaFile.removeFile(cordova.file.cacheDirectory,file);
            }

          }, function () {

            console.log('cordovaZip error');

          }, function (progressEvent) {

            console.log(progressEvent);

            }
          );

      },  function (error) {

        console.log('Error with cordovaFileTransfer');

      },  function (progress) {

        $timeout(function () {
          $scope.downloadProgress = ((progress.loaded / progress.total) * 100) + '%';

      });

    });

  };

结果总是'cordovaZip error',如果尝试将任何参数传递给函数(错误的一个),则不返回任何对象。

1 个答案:

答案 0 :(得分:0)

看起来您的路径不正确

    $cordovaZip
      .unzip(
        result.nativeURL,
        cordova.file.dataDirectory 
      ).then(function () {

应该或可能

    $cordovaZip
      .unzip(
        targetPath,
        cordova.file.dataDirectory 
      ).then(function () {

不确定nativeURL给出了什么我会安排所有这些以查看匹配 - 也认为nativeURL是一个方法nativeURL()