10
10
/**
11
11
* The angular file upload module
12
12
* @author : nerv
13
- * @version : 0.2.9.8.1, 2013-12-31
13
+ * @version : 0.3, 2014-01-03
14
14
*/
15
15
var app = angular . module ( 'angularFileUpload' , [ ] ) ;
16
16
17
17
/**
18
18
* The angular file upload module
19
19
* @author : nerv
20
- * @version : 0.2.9.8.1, 2013-12-31
20
+ * @version : 0.3, 2014-01-03
21
21
*/
22
22
23
23
// It is attached to an element that catches the event drop file
@@ -57,7 +57,7 @@ app.directive('ngFileDrop', [ '$fileUploader', function ($fileUploader) {
57
57
/**
58
58
* The angular file upload module
59
59
* @author : nerv
60
- * @version : 0.2.9.8.1, 2013-12-31
60
+ * @version : 0.3, 2014-01-03
61
61
*/
62
62
63
63
// 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 () {
78
78
/**
79
79
* The angular file upload module
80
80
* @author : nerv
81
- * @version : 0.2.9.8.1, 2013-12-31
81
+ * @version : 0.3, 2014-01-03
82
82
*/
83
83
84
84
// It is attached to <input type="file"> element like <ng-file-select="options">
@@ -90,18 +90,19 @@ app.directive('ngFileSelect', [ '$fileUploader', function ($fileUploader) {
90
90
$fileUploader . isHTML5 || element . removeAttr ( 'multiple' ) ;
91
91
92
92
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 ) ) ;
94
94
$fileUploader . isHTML5 && element . prop ( 'value' , null ) ;
95
95
} ) ;
96
+
97
+ element . prop ( 'value' , null ) ; // FF fix
96
98
}
97
99
} ;
98
100
} ] ) ;
99
101
/**
100
102
* The angular file upload module
101
103
* @author : nerv
102
- * @version : 0.2.9.8.1, 2013-12-31
104
+ * @version : 0.3, 2014-01-03
103
105
*/
104
-
105
106
app . factory ( '$fileUploader' , [ '$compile' , '$rootScope' , '$http' , '$window' , function ( $compile , $rootScope , $http , $window ) {
106
107
'use strict' ;
107
108
@@ -181,27 +182,27 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
181
182
182
183
/**
183
184
* Adds items to the queue
184
- * @param {FileList|File|Input } items
185
+ * @param {FileList|File|HTMLInputElement } items
185
186
* @param {Object } [options]
186
187
*/
187
188
addToQueue : function ( items , options ) {
188
189
var length = this . queue . length ;
189
190
190
- angular . forEach ( 'length' in items ? items : [ items ] , function ( item ) {
191
+ angular . forEach ( 'length' in items ? items : [ items ] , function ( file ) {
191
192
var isValid = ! this . filters . length ? true : this . filters . every ( function ( filter ) {
192
- return filter . call ( this , item ) ;
193
+ return filter . call ( this , file ) ;
193
194
} , this ) ;
194
195
195
196
if ( isValid ) {
196
- item = new Item ( angular . extend ( {
197
+ var item = new Item ( angular . extend ( {
197
198
url : this . url ,
198
199
alias : this . alias ,
199
200
headers : angular . copy ( this . headers ) ,
200
201
formData : angular . copy ( this . formData ) ,
201
202
removeAfterUpload : this . removeAfterUpload ,
202
203
method : this . method ,
203
204
uploader : this ,
204
- file : item
205
+ file : file
205
206
} , options ) ) ;
206
207
207
208
this . queue . push ( item ) ;
@@ -225,7 +226,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
225
226
var item = this . queue [ index ] ;
226
227
item . isUploading && item . cancel ( ) ;
227
228
this . queue . splice ( index , 1 ) ;
228
- item . _destroyForm ( ) ;
229
+ item . _destroy ( ) ;
229
230
this . trigger ( 'changedqueue' , item ) ;
230
231
} ,
231
232
@@ -235,7 +236,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
235
236
clearQueue : function ( ) {
236
237
this . queue . forEach ( function ( item ) {
237
238
item . isUploading && item . cancel ( ) ;
238
- item . _destroyForm ( ) ;
239
+ item . _destroy ( ) ;
239
240
} , this ) ;
240
241
this . queue . length = 0 ;
241
242
this . trigger ( 'changedqueue' , this . queue ) ;
@@ -302,12 +303,8 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
302
303
cancelItem : function ( value ) {
303
304
var index = this . getIndexOfItem ( value ) ;
304
305
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 ( ) ;
311
308
} ,
312
309
313
310
@@ -442,17 +439,15 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
442
439
* The IFrame transport
443
440
*/
444
441
_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 ;
448
445
var that = this ;
449
446
450
- this . trigger ( 'beforeupload' , item ) ;
447
+ item . _form && item . _form . replaceWith ( input ) ; // remove old form
448
+ item . _form = form ; // save link to new form
451
449
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 ) ;
456
451
457
452
input . prop ( 'name' , item . alias ) ;
458
453
@@ -470,12 +465,24 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
470
465
encoding : 'multipart/form-data' // old IE
471
466
} ) ;
472
467
473
- iframe . unbind ( ) . bind ( 'load' , function ( ) {
468
+ iframe . bind ( 'load' , function ( ) {
474
469
var xhr = { response : iframe . contents ( ) [ 0 ] . body . innerHTML , status : 200 , dummy : true } ;
475
470
var response = that . _transformResponse ( xhr . response ) ;
471
+ that . trigger ( 'in:success' , xhr , item , response ) ;
476
472
that . trigger ( 'in:complete' , xhr , item , response ) ;
477
473
} ) ;
478
474
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
+
479
486
form [ 0 ] . submit ( ) ;
480
487
} ,
481
488
@@ -510,20 +517,18 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
510
517
if ( ! Uploader . prototype . isHTML5 ) {
511
518
var input = angular . element ( params . file ) ;
512
519
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 ( ) + '">' ) ;
515
520
var value = input . val ( ) ;
516
521
517
522
params . file = {
518
523
lastModifiedDate : null ,
519
524
size : null ,
520
525
type : 'like/' + value . replace ( / ^ .+ \. (? ! \. ) | .* / , '' ) ,
521
- name : value . match ( / [ ^ \\ ] + $ / ) [ 0 ] ,
522
- _form : form
526
+ name : value . match ( / [ ^ \\ ] + $ / ) [ 0 ]
523
527
} ;
524
528
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 ) ;
527
532
}
528
533
529
534
angular . extend ( this , {
@@ -548,11 +553,11 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
548
553
cancel : function ( ) {
549
554
this . uploader . cancelItem ( this ) ;
550
555
} ,
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 ;
556
561
} ,
557
562
_beforeupload : function ( event , item ) {
558
563
item . isReady = true ;
@@ -561,7 +566,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
561
566
item . isSuccess = false ;
562
567
item . isCancel = false ;
563
568
item . isError = false ;
564
- item . progress = null ;
569
+ item . progress = 0 ;
565
570
} ,
566
571
_progress : function ( event , item , progress ) {
567
572
item . progress = progress ;
0 commit comments