2016-01-07 18 views
1

有谁知道如何同时使用$ cordovaFileTransfer插件与离子中止下载?

我有当用户下载文件的选项一个观点,但我要确保,如果他们开始下载文件,但随后按“返回”按钮,取消下载,基本重置视图。当下载开始和视图留什么似乎是发生的是,下载仍然没有它的事,尽管我试图做这样的事情代码:

// If the user leaves, check if a download is in progress. 
    // - If so, reset the variable, and remove the directory 
    $scope.$on('$ionicView.leave', function() { 
     if($scope.downloading === true) { 
     $scope.downloading = false; 

     $cordovaFile.removeRecursively(cordova.file.dataDirectory, courseDir) 
     .then(function (success) { 
     }); 
     } 
    }); 

我已经打过电话$ cordovaFileTransfer.abort()它告诉我没有这样的函数,即使我在插件中看到一个中止函数本身。有没有人对此有所了解,因为我认为这将是人们寻找的常见功能。

谢谢!

回答

1

我最终通过进入NG-cordova.js文件直接和添加abort函数像这样解决这个问题:

我做了一个叫工厂对象的内部英尺全局变量和删除变量实例在“下载”功能中,以使其对所有功能具有全局性。

/* globals FileTransfer: true */ 
angular.module('ngCordova.plugins.fileTransfer', []) 

    .factory('$cordovaFileTransfer', ['$q', '$timeout', function ($q, $timeout) { 
    var ft = new FileTransfer(); 
    return { 
     download: function (source, filePath, options, trustAllHosts) { 
     var q = $q.defer(); 
     var uri = (options && options.encodeURI === false) ? source : encodeURI(source); 

     if (options && options.timeout !== undefined && options.timeout !== null) { 
      $timeout(function() { 
      ft.abort(); 
      }, options.timeout); 
      options.timeout = null; 
     } 

     ft.onprogress = function (progress) { 
      q.notify(progress); 
     }; 

     q.promise.abort = function() { 
      ft.abort(); 
     }; 

     ft.download(uri, filePath, q.resolve, q.reject, trustAllHosts, options); 
     return q.promise; 
     }, 

     upload: function (server, filePath, options, trustAllHosts) { 
     var q = $q.defer(); 

     var uri = (options && options.encodeURI === false) ? server : encodeURI(server); 

     if (options && options.timeout !== undefined && options.timeout !== null) { 
      $timeout(function() { 
      ft.abort(); 
      }, options.timeout); 
      options.timeout = null; 
     } 

     ft.onprogress = function (progress) { 
      q.notify(progress); 
     }; 

     q.promise.abort = function() { 
      ft.abort(); 
     }; 

     ft.upload(filePath, uri, q.resolve, q.reject, options, trustAllHosts); 
     return q.promise; 
     }, 

     /* Here is the added abort function that will 
     kill the download or upload by calling $cordovaFileTransfer.abort() */ 
     abort: function() { 
     var q = $q.defer; 
     ft.abort(); 
     q.resolve; 
     return q.promise; 
     } 
    }; 
}]); 
+0

如果有多个下载会发生什么? –

0

中止被添加到$ cordovaFileTransfer的下载方法的新版本,让喜欢用没有问题了以下工作,如果你升级你的ngcordova代码:

var transferPromise = $cordovaFileTransfer.download(url, targetPath, {}, true); 
transferPromise.then(
    function() { },     
    function() { }, 
    function (progress) { } 
    ); 
. 
. 
. 
transferPromise.abort(); 
+0

这似乎不再是在NGCordova文件中的方法 - https://github.com/driftyco/ng-cordova/blob/master/src/plugins/fileTransfer.js –

+1

嗨,它在那里。下载(...)返回一个承诺并放弃()添加到该承诺。检查线25 – Sep

+0

我不能得到这个工作 - 做 - $ cordovaFileTransfer.fileDownloading = $ cordovaFileTransfer.download(FULLPATH,路径选择,trustHosts) 。然后((函数(结果){})(函数(错误){}),函数(progress){},false); 然后再调用$ cordovaFileTransfer.fileDownloading.abort()说没有功能 - 也许我正在处理承诺 - 并返回此... –