ngCordova$cordovaFileTransfer-如何中止下载?abort()未定义
有人知道如何在使用带有ionic的$cordovaFileTransfer插件时中止下载吗 我有一个视图,用户可以选择下载文件,但我想确保如果他们开始下载文件,但按下“后退”按钮,它会取消下载并基本重置视图。似乎正在发生的事情是,当下载开始,视图离开时,下载仍然完成它的工作,尽管我试图在代码中执行类似的操作:ngCordova$cordovaFileTransfer-如何中止下载?abort()未定义,cordova,ionic,ngcordova,Cordova,Ionic,Ngcordova,有人知道如何在使用带有ionic的$cordovaFileTransfer插件时中止下载吗 我有一个视图,用户可以选择下载文件,但我想确保如果他们开始下载文件,但按下“后退”按钮,它会取消下载并基本重置视图。似乎正在发生的事情是,当下载开始,视图离开时,下载仍然完成它的工作,尽管我试图在代码中执行类似的操作: // If the user leaves, check if a download is in progress. // - If so, reset the variable,
// 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(),它告诉我没有这样的函数,尽管我在插件中看到了一个abort函数。有人对此有什么见解吗?因为我觉得这是人们正在寻找的一种常见功能
谢谢 我通过直接进入ng-cordova.js文件并添加abort函数来解决这个问题,如下所示: 我在factory对象内部创建了一个名为ft的全局变量,并删除了“download”函数中的变量实例化,以使其对所有函数都是全局的
/* 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;
}
};
}]);
Abort被添加到更新版本的$cordovaFileTransfer下载方法中,因此,如果升级ngcordova,下面这样的代码可以正常工作:
var transferPromise = $cordovaFileTransfer.download(url, targetPath, {}, true);
transferPromise.then(
function () { },
function () { },
function (progress) { }
);
.
.
.
transferPromise.abort();
这似乎不再是NGCordova文件中的方法-嗨,它就在那里。下载(…)返回一个承诺,并将abort()添加到该承诺中。检查第25行。我无法让它工作-执行-$cordovfiletransfer.fileDownloading=$cordovfiletransfer.download(完整路径、路径、选项、信任主机)。然后((函数(结果){}),(函数(错误){}),函数(进度){},false);然后调用$cordovaFileTransfer.fileDownloading.abort()表示没有函数-可能我正在处理承诺-并返回此…我为您编辑了答案。请注意,您应该首先获得承诺,然后调用,然后()方法。如果有多个下载,会发生什么情况?