Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit d2a9407

Browse files
BrandonNoadPowerKiKi
authored andcommitted
Restored the original behaviour of onCompleted() and onCompletedAll()
Restored the original behaviour of onCompleted() and onCompletedAll(); added new onUploadSuccess() callback FIX #25, #26
1 parent a473389 commit d2a9407

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,18 @@ $uiUploader.startUpload({
6565
// file contains a File object
6666
console.log(file);
6767
},
68-
onCompleted: function(file, response) {
68+
onUploadSuccess: function(file) {
6969
// file contains a File object
7070
console.log(file);
71-
// response contains the server response
72-
console.log(response);
73-
}
71+
},
72+
onCompleted: function(file, responseText, status) {
73+
// file contains a File object
74+
console.log(file);
75+
// responseText contains the server response as text
76+
console.log(responseText);
77+
// status contains the status of the response
78+
console.log(status);
79+
},
7480
onCompletedAll: function(files) {
7581
// files is an array of File objects
7682
console.log(files);
@@ -87,11 +93,14 @@ $uiUploader.startUpload({
8793
withCredentials: true
8894
},
8995
onProgress: function(file) {
90-
//do stuff
96+
// do stuff
9197
},
92-
onCompleted: function(file, response) {
98+
onUploadSuccess: function(file) {
9399
// do stuff
94-
}
100+
},
101+
onCompleted: function(file, responseText, status) {
102+
// do stuff
103+
},
95104
onCompletedAll: function(files) {
96105
// do stuff
97106
}

src/uploader.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,34 @@ function uiUploader($log) {
112112
}
113113
};
114114

115-
// Triggered when upload is completed:
115+
// Triggered when the upload is successful (the server may not have responded yet).
116116
xhr.upload.onload = function() {
117+
118+
if (angular.isFunction(self.options.onUploadSuccess)) {
119+
self.options.onUploadSuccess(file);
120+
}
121+
};
122+
123+
// Triggered when upload fails:
124+
xhr.upload.onerror = function(e) {
125+
if (angular.isFunction(self.options.onError)) {
126+
self.options.onError(e);
127+
}
128+
};
129+
130+
// Triggered when the upload has completed AND the server has responded. Equivalent to
131+
// listening for the readystatechange event when xhr.readyState === XMLHttpRequest.DONE.
132+
xhr.onload = function () {
133+
117134
self.activeUploads -= 1;
118135
self.uploadedFiles += 1;
136+
119137
startUpload(self.options);
138+
120139
if (angular.isFunction(self.options.onCompleted)) {
121140
self.options.onCompleted(file, xhr.responseText, xhr.status);
122-
}
141+
}
142+
123143
if (self.activeUploads === 0) {
124144
self.uploadedFiles = 0;
125145
if (angular.isFunction(self.options.onCompletedAll)) {
@@ -128,13 +148,6 @@ function uiUploader($log) {
128148
}
129149
};
130150

131-
// Triggered when upload fails:
132-
xhr.upload.onerror = function(e) {
133-
if (angular.isFunction(self.options.onError)) {
134-
self.options.onError(e);
135-
}
136-
};
137-
138151
// Append additional data if provided:
139152
if (data) {
140153
for (prop in data) {

0 commit comments

Comments
 (0)