Skip to content

Commit a339cae

Browse files
author
nerv
committed
Added: Canceling uploading for old browsers (Issue nervgh#28, part 2)
1 parent 88bf47c commit a339cae

File tree

9 files changed

+95
-85
lines changed

9 files changed

+95
-85
lines changed

angular-file-upload.js

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
/**
1111
* The angular file upload module
1212
* @author: nerv
13-
* @version: 0.2.9.8.1, 2013-12-31
13+
* @version: 0.3, 2014-01-03
1414
*/
1515
var app = angular.module('angularFileUpload', []);
1616

1717
/**
1818
* The angular file upload module
1919
* @author: nerv
20-
* @version: 0.2.9.8.1, 2013-12-31
20+
* @version: 0.3, 2014-01-03
2121
*/
2222

2323
// It is attached to an element that catches the event drop file
@@ -57,7 +57,7 @@ app.directive('ngFileDrop', [ '$fileUploader', function ($fileUploader) {
5757
/**
5858
* The angular file upload module
5959
* @author: nerv
60-
* @version: 0.2.9.8.1, 2013-12-31
60+
* @version: 0.3, 2014-01-03
6161
*/
6262

6363
// It is attached to an element which will be assigned to a class "ng-file-over" or ng-file-over="className"
@@ -78,7 +78,7 @@ app.directive('ngFileOver', function () {
7878
/**
7979
* The angular file upload module
8080
* @author: nerv
81-
* @version: 0.2.9.8.1, 2013-12-31
81+
* @version: 0.3, 2014-01-03
8282
*/
8383

8484
// It is attached to <input type="file"> element like <ng-file-select="options">
@@ -90,18 +90,19 @@ app.directive('ngFileSelect', [ '$fileUploader', function ($fileUploader) {
9090
$fileUploader.isHTML5 || element.removeAttr('multiple');
9191

9292
element.bind('change', function () {
93-
scope.$emit('file:add', this.files ? this.files : this, scope.$eval(attributes.ngFileSelect));
93+
scope.$emit('file:add', $fileUploader.isHTML5 ? this.files : this, scope.$eval(attributes.ngFileSelect));
9494
$fileUploader.isHTML5 && element.prop('value', null);
9595
});
96+
97+
element.prop('value', null); // FF fix
9698
}
9799
};
98100
}]);
99101
/**
100102
* The angular file upload module
101103
* @author: nerv
102-
* @version: 0.2.9.8.1, 2013-12-31
104+
* @version: 0.3, 2014-01-03
103105
*/
104-
105106
app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', function ($compile, $rootScope, $http, $window) {
106107
'use strict';
107108

@@ -181,27 +182,27 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
181182

182183
/**
183184
* Adds items to the queue
184-
* @param {FileList|File|Input} items
185+
* @param {FileList|File|HTMLInputElement} items
185186
* @param {Object} [options]
186187
*/
187188
addToQueue: function (items, options) {
188189
var length = this.queue.length;
189190

190-
angular.forEach('length' in items ? items : [ items ], function (item) {
191+
angular.forEach('length' in items ? items : [ items ], function (file) {
191192
var isValid = !this.filters.length ? true : this.filters.every(function (filter) {
192-
return filter.call(this, item);
193+
return filter.call(this, file);
193194
}, this);
194195

195196
if (isValid) {
196-
item = new Item(angular.extend({
197+
var item = new Item(angular.extend({
197198
url: this.url,
198199
alias: this.alias,
199200
headers: angular.copy(this.headers),
200201
formData: angular.copy(this.formData),
201202
removeAfterUpload: this.removeAfterUpload,
202203
method: this.method,
203204
uploader: this,
204-
file: item
205+
file: file
205206
}, options));
206207

207208
this.queue.push(item);
@@ -225,7 +226,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
225226
var item = this.queue[ index ];
226227
item.isUploading && item.cancel();
227228
this.queue.splice(index, 1);
228-
item._destroyForm();
229+
item._destroy();
229230
this.trigger('changedqueue', item);
230231
},
231232

@@ -235,7 +236,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
235236
clearQueue: function () {
236237
this.queue.forEach(function (item) {
237238
item.isUploading && item.cancel();
238-
item._destroyForm();
239+
item._destroy();
239240
}, this);
240241
this.queue.length = 0;
241242
this.trigger('changedqueue', this.queue);
@@ -302,12 +303,8 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
302303
cancelItem: function(value) {
303304
var index = this.getIndexOfItem(value);
304305
var item = this.queue[ index ];
305-
306-
if (this.isHTML5) {
307-
item._xhr && item._xhr.abort();
308-
} else {
309-
// TODO: old browsers
310-
}
306+
var prop = this.isHTML5 ? '_xhr' : '_form';
307+
item[prop] && item[prop].abort();
311308
},
312309

313310

@@ -442,17 +439,15 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
442439
* The IFrame transport
443440
*/
444441
_iframeTransport: function (item) {
445-
var form = item.file._form;
446-
var iframe = form.find('iframe');
447-
var input = form.find('input');
442+
var form = angular.element('<form style="display: none;" />');
443+
var iframe = angular.element('<iframe name="iframeTransport' + Date.now() + '">');
444+
var input = item._input;
448445
var that = this;
449446

450-
this.trigger('beforeupload', item);
447+
item._form && item._form.replaceWith(input); // remove old form
448+
item._form = form; // save link to new form
451449

452-
// remove all but the INPUT file type
453-
angular.forEach(input, function(element) {
454-
element.type !== 'file' && angular.element(element).remove(); // prevent memory leaks
455-
});
450+
this.trigger('beforeupload', item);
456451

457452
input.prop('name', item.alias);
458453

@@ -470,12 +465,24 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
470465
encoding: 'multipart/form-data' // old IE
471466
});
472467

473-
iframe.unbind().bind('load', function () {
468+
iframe.bind('load', function () {
474469
var xhr = { response: iframe.contents()[ 0 ].body.innerHTML, status: 200, dummy: true };
475470
var response = that._transformResponse(xhr.response);
471+
that.trigger('in:success', xhr, item, response);
476472
that.trigger('in:complete', xhr, item, response);
477473
});
478474

475+
form.abort = function() {
476+
var xhr = { status: 0, dummy: true };
477+
iframe.unbind('load').prop('src', 'javascript:false;');
478+
form.replaceWith(input);
479+
that.trigger('in:cancel', xhr, item);
480+
that.trigger('in:complete', xhr, item);
481+
};
482+
483+
input.after(form);
484+
form.append(input).append(iframe);
485+
479486
form[ 0 ].submit();
480487
},
481488

@@ -510,20 +517,18 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
510517
if (!Uploader.prototype.isHTML5) {
511518
var input = angular.element(params.file);
512519
var clone = $compile(input.clone())(params.uploader.scope);
513-
var form = angular.element('<form style="display: none;" />');
514-
var iframe = angular.element('<iframe name="iframeTransport' + Date.now() + '">');
515520
var value = input.val();
516521

517522
params.file = {
518523
lastModifiedDate: null,
519524
size: null,
520525
type: 'like/' + value.replace(/^.+\.(?!\.)|.*/, ''),
521-
name: value.match(/[^\\]+$/)[ 0 ],
522-
_form: form
526+
name: value.match(/[^\\]+$/)[ 0 ]
523527
};
524528

525-
input.after(clone).after(form);
526-
form.append(input).append(iframe);
529+
params._input = input;
530+
clone.prop('value', null); // FF fix
531+
input.hide().after(clone);
527532
}
528533

529534
angular.extend(this, {
@@ -548,11 +553,11 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
548553
cancel: function() {
549554
this.uploader.cancelItem(this);
550555
},
551-
_hasForm: function() {
552-
return !!(this.file && this.file._form);
553-
},
554-
_destroyForm: function() {
555-
this._hasForm() && this.file._form.remove();
556+
_destroy: function() {
557+
this._form && this._form.remove();
558+
this._input && this._input.remove();
559+
delete this._form;
560+
delete this._input;
556561
},
557562
_beforeupload: function (event, item) {
558563
item.isReady = true;
@@ -561,7 +566,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
561566
item.isSuccess = false;
562567
item.isCancel = false;
563568
item.isError = false;
564-
item.progress = null;
569+
item.progress = 0;
565570
},
566571
_progress: function (event, item, progress) {
567572
item.progress = progress;

angular-file-upload.min.js

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

angular-file-upload.min.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.

bower.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": "0.2.9.8.1",
3+
"version": "0.3",
44
"main": "angular-file-upload.js",
55
"ignore": [],
66
"dependencies": {

src/scripts/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* The angular file upload module
33
* @author: nerv
4-
* @version: 0.2.9.8.1, 2013-12-31
4+
* @version: 0.3, 2014-01-03
55
*/
66
var app = angular.module('angularFileUpload', []);

src/scripts/directives/ngFileDrop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* The angular file upload module
33
* @author: nerv
4-
* @version: 0.2.9.8.1, 2013-12-31
4+
* @version: 0.3, 2014-01-03
55
*/
66

77
// It is attached to an element that catches the event drop file

src/scripts/directives/ngFileOver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* The angular file upload module
33
* @author: nerv
4-
* @version: 0.2.9.8.1, 2013-12-31
4+
* @version: 0.3, 2014-01-03
55
*/
66

77
// It is attached to an element which will be assigned to a class "ng-file-over" or ng-file-over="className"

src/scripts/directives/ngFileSelect.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* The angular file upload module
33
* @author: nerv
4-
* @version: 0.2.9.8.1, 2013-12-31
4+
* @version: 0.3, 2014-01-03
55
*/
66

77
// It is attached to <input type="file"> element like <ng-file-select="options">
@@ -13,9 +13,11 @@ app.directive('ngFileSelect', [ '$fileUploader', function ($fileUploader) {
1313
$fileUploader.isHTML5 || element.removeAttr('multiple');
1414

1515
element.bind('change', function () {
16-
scope.$emit('file:add', this.files ? this.files : this, scope.$eval(attributes.ngFileSelect));
16+
scope.$emit('file:add', $fileUploader.isHTML5 ? this.files : this, scope.$eval(attributes.ngFileSelect));
1717
$fileUploader.isHTML5 && element.prop('value', null);
1818
});
19+
20+
element.prop('value', null); // FF fix
1921
}
2022
};
2123
}]);

0 commit comments

Comments
 (0)