@@ -316,6 +316,12 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
316316 */
317317 onCompleteItem ( item , response , status , headers ) {
318318 }
319+ /**
320+ * Callback
321+ * @param {FileItem } item
322+ */
323+ onTimeoutItem ( item ) {
324+ }
319325 /**
320326 * Callback
321327 */
@@ -507,6 +513,15 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
507513 this . _onCompleteItem ( item , response , xhr . status , headers ) ;
508514 } ;
509515
516+ xhr . timeout = item . timeout || 0 ;
517+
518+ xhr . ontimeout = ( e ) => {
519+ var headers = this . _parseHeaders ( xhr . getAllResponseHeaders ( ) ) ;
520+ var response = "Request Timeout." ;
521+ this . _onTimeoutItem ( item ) ;
522+ this . _onCompleteItem ( item , response , 408 , headers ) ;
523+ } ;
524+
510525 xhr . open ( item . method , item . url , true ) ;
511526
512527 xhr . withCredentials = item . withCredentials ;
@@ -527,6 +542,10 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
527542 var iframe = element ( '<iframe name="iframeTransport' + Date . now ( ) + '">' ) ;
528543 var input = item . _input ;
529544
545+ var timeout = 0 ;
546+ var timer = null ;
547+ var isTimedOut = false ;
548+
530549 if ( item . _form ) item . _form . replaceWith ( input ) ; // remove old form
531550 item . _form = form ; // save link to new form
532551
@@ -572,6 +591,15 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
572591 status = 500 ;
573592 }
574593
594+ if ( timer ) {
595+ clearTimeout ( timer ) ;
596+ }
597+ timer = null ;
598+
599+ if ( isTimedOut ) {
600+ return false ; //throw 'Request Timeout'
601+ }
602+
575603 var xhr = { response : html , status : status , dummy : true } ;
576604 var headers = { } ;
577605 var response = this . _transformResponse ( xhr . response , headers ) ;
@@ -595,6 +623,26 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
595623 input . after ( form ) ;
596624 form . append ( input ) . append ( iframe ) ;
597625
626+ timeout = item . timeout || 0 ;
627+ timer = null ;
628+
629+ if ( timeout ) {
630+ timer = setTimeout ( ( ) => {
631+ isTimedOut = true ;
632+
633+ item . isCancel = true ;
634+ if ( item . isUploading ) {
635+ iframe . unbind ( 'load' ) . prop ( 'src' , 'javascript:false;' ) ;
636+ form . replaceWith ( input ) ;
637+ }
638+
639+ var headers = { } ;
640+ var response = "Request Timeout." ;
641+ this . _onTimeoutItem ( item ) ;
642+ this . _onCompleteItem ( item , response , 408 , headers ) ;
643+ } , timeout ) ;
644+ }
645+
598646 form [ 0 ] . submit ( ) ;
599647 }
600648 /**
@@ -704,6 +752,15 @@ export default function __identity(fileUploaderOptions, $rootScope, $http, $wind
704752 this . progress = this . _getTotalProgress ( ) ;
705753 this . _render ( ) ;
706754 }
755+ /**
756+ * Inner callback
757+ * @param {FileItem } item
758+ * @private
759+ */
760+ _onTimeoutItem ( item ) {
761+ item . _onTimeout ( ) ;
762+ this . onTimeoutItem ( item ) ;
763+ }
707764 /**********************
708765 * STATIC
709766 **********************/
0 commit comments