Skip to content

Commit 4f50057

Browse files
committed
Fixed: "Can not cancel upload onBeforeUploadItem." (#425)
1 parent 5d5d8fa commit 4f50057

File tree

7 files changed

+78
-51
lines changed

7 files changed

+78
-51
lines changed

dist/angular-file-upload.js

Lines changed: 52 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-file-upload.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-file-upload.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-file-upload.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-file-upload",
3-
"version": "2.3.2",
3+
"version": "2.3.3",
44
"homepage": "https://github.com/nervgh/angular-file-upload",
55
"description": "Angular File Upload is a module for the AngularJS framework",
66
"author": {

src/services/FileItem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default function __identity($compile, FileLikeObject) {
133133
*/
134134
_onBeforeUpload() {
135135
this.isReady = true;
136-
this.isUploading = true;
136+
this.isUploading = false;
137137
this.isUploaded = false;
138138
this.isSuccess = false;
139139
this.isCancel = false;

src/services/FileUploader.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let {
1616
} = angular;
1717

1818

19-
export default function __identity(fileUploaderOptions, $rootScope, $http, $window, FileLikeObject, FileItem) {
19+
export default function __identity(fileUploaderOptions, $rootScope, $http, $window, $timeout, FileLikeObject, FileItem) {
2020

2121

2222
let {
@@ -115,8 +115,13 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
115115
item._prepareToUploading();
116116
if(this.isUploading) return;
117117

118+
this._onBeforeUploadItem(item);
119+
if (item.isCancel) return;
120+
121+
item.isUploading = true;
118122
this.isUploading = true;
119123
this[transport](item);
124+
this._render();
120125
}
121126
/**
122127
* Cancels uploading of item from the queue
@@ -126,7 +131,19 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
126131
var index = this.getIndexOfItem(value);
127132
var item = this.queue[index];
128133
var prop = this.isHTML5 ? '_xhr' : '_form';
129-
if(item && item.isUploading) item[prop].abort();
134+
if (!item) return;
135+
item.isCancel = true;
136+
if(item.isUploading) {
137+
// It will call this._onCancelItem() & this._onCompleteItem() asynchronously
138+
item[prop].abort();
139+
} else {
140+
let dummy = [undefined, 0, {}];
141+
let onNextTick = () => {
142+
this._onCancelItem(item, ...dummy);
143+
this._onCompleteItem(item, ...dummy);
144+
};
145+
$timeout(onNextTick); // Trigger callbacks asynchronously (setImmediate emulation)
146+
}
130147
}
131148
/**
132149
* Uploads all not uploaded items of queue
@@ -426,8 +443,6 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
426443
var xhr = item._xhr = new XMLHttpRequest();
427444
var sendable;
428445

429-
this._onBeforeUploadItem(item);
430-
431446
if (!item.disableMultipart) {
432447
sendable = new FormData();
433448
forEach(item.formData, (obj) => {
@@ -483,7 +498,6 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
483498
});
484499

485500
xhr.send(sendable);
486-
this._render();
487501
}
488502
/**
489503
* The IFrame transport
@@ -498,8 +512,6 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
498512
if(item._form) item._form.replaceWith(input); // remove old form
499513
item._form = form; // save link to new form
500514

501-
this._onBeforeUploadItem(item);
502-
503515
input.prop('name', item.alias);
504516

505517
forEach(item.formData, (obj) => {
@@ -566,7 +578,6 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
566578
form.append(input).append(iframe);
567579

568580
form[0].submit();
569-
this._render();
570581
}
571582
/**
572583
* Inner callback
@@ -744,6 +755,7 @@ __identity.$inject = [
744755
'$rootScope',
745756
'$http',
746757
'$window',
758+
'$timeout',
747759
'FileLikeObject',
748760
'FileItem'
749761
];

0 commit comments

Comments
 (0)