app factory filesUpload upload timeout function upload timeout return

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
app.factory('$filesUpload', ['$upload', '$timeout', function ($upload, $timeout) {
return function($scope, uploadUrl, uploadData, successCallback) {
$scope.hasUploader = function(index) {
return $scope.upload[index] != null;
};
$scope.abort = function(index) {
$scope.upload[index].abort();
$scope.upload[index] = null;
};
$scope.onFileSelect = function($files) {//asdf
$scope.uploadedFiles = [];
$scope.progress = [];
if ($scope.upload && $scope.upload.length > 0) {
for (var i = 0; i < $scope.upload.length; i++) {
if ($scope.upload[i] != null) {
$scope.upload[i].abort();
}
}
}
$scope.upload = [];
$scope.uploadedFiles = $files;
for ( var i = 0; i < $files.length; i++) {
$scope.progress[i] = -1;
$scope.start(i);
}
};
$scope.start = function(index) {
$scope.progress[index] = 0;
$scope.errorMsg = null;
$scope.upload[index] = $upload.upload({
url: uploadUrl,
data: uploadData(),
transformRequest: function(data) {
return data
},
file: $scope.uploadedFiles[index]
});
$scope.upload[index].then(function(response) {
$timeout(function() {
successCallback(response);
});
}, function(response) {
if (response.status > 0) $scope.errorMsg = response.status + ': ' + response.data;
}, function(evt) {
// Math.min is to fix IE which reports 200% sometimes
$scope.progress[index] = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
// .xhr(function(xhr){
// xhr.upload.addEventListener('abort', function() {console.log('abort complete')}, false);
// });
};
};
}]);