Skip to content

Commit 7f65dd8

Browse files
committed
support timeout
1 parent edaae54 commit 7f65dd8

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed

examples/simple/controllers.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ angular
1010
.controller('AppController', ['$scope', 'FileUploader', function($scope, FileUploader) {
1111
var uploader = $scope.uploader = new FileUploader({
1212
url: 'upload.php'
13+
//,timeout: 2000
1314
});
1415

1516
// FILTERS
@@ -64,6 +65,11 @@ angular
6465
uploader.onCompleteItem = function(fileItem, response, status, headers) {
6566
console.info('onCompleteItem', fileItem, response, status, headers);
6667
};
68+
69+
uploader.onTimeoutItem = function(fileItem) {
70+
console.info('onTimeoutItem', fileItem);
71+
};
72+
6773
uploader.onCompleteAll = function() {
6874
console.info('onCompleteAll');
6975
};

examples/simple/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ <h3>Select files</h3>
7878
<input type="file" nv-file-select="" uploader="uploader" multiple /><br/>
7979

8080
Single
81-
<input type="file" nv-file-select="" uploader="uploader" />
81+
<input type="file" nv-file-select="" uploader="uploader" /><br/>
82+
83+
Set timeout 2s
84+
<input type="file" nv-file-select="" uploader="uploader" options="{ timeout: 2000 }"/>
8285
</div>
8386

8487
<div class="col-md-9" style="margin-bottom: 40px">

src/services/FileItem.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export default function __identity($compile, FileLikeObject) {
2424
* @constructor
2525
*/
2626
constructor(uploader, some, options) {
27-
var isInput = isElement(some);
28-
var input = isInput ? element(some) : null;
27+
var isInput = !!some.input;
28+
var input = isInput ? element(some.input) : null;
2929
var file = !isInput ? some : null;
3030

3131
extend(this, {
@@ -36,7 +36,8 @@ export default function __identity($compile, FileLikeObject) {
3636
removeAfterUpload: uploader.removeAfterUpload,
3737
withCredentials: uploader.withCredentials,
3838
disableMultipart: uploader.disableMultipart,
39-
method: uploader.method
39+
method: uploader.method,
40+
timeout: uploader.timeout
4041
}, options, {
4142
uploader: uploader,
4243
file: new FileLikeObject(some),
@@ -126,6 +127,11 @@ export default function __identity($compile, FileLikeObject) {
126127
*/
127128
onComplete(response, status, headers) {
128129
}
130+
/**
131+
* Callback
132+
*/
133+
onTimeout() {
134+
}
129135
/**********************
130136
* PRIVATE
131137
**********************/
@@ -216,6 +222,21 @@ export default function __identity($compile, FileLikeObject) {
216222
this.onComplete(response, status, headers);
217223
if(this.removeAfterUpload) this.remove();
218224
}
225+
/**
226+
* Inner callback
227+
* @private
228+
*/
229+
_onTimeout() {
230+
this.isReady = false;
231+
this.isUploading = false;
232+
this.isUploaded = false;
233+
this.isSuccess = false;
234+
this.isCancel = false;
235+
this.isError = true;
236+
this.progress = 0;
237+
this.index = null;
238+
this.onTimeout();
239+
}
219240
/**
220241
* Destroys a FileItem
221242
*/

src/services/FileLikeObject.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ export default function __identity() {
2525
var fakePathOrObject = isInput ? fileOrInput.value : fileOrInput;
2626
var postfix = isString(fakePathOrObject) ? 'FakePath' : 'Object';
2727
var method = '_createFrom' + postfix;
28-
this[method](fakePathOrObject);
28+
this[method](fakePathOrObject, fileOrInput);
2929
}
3030
/**
3131
* Creates file like object from fake path string
3232
* @param {String} path
3333
* @private
3434
*/
35-
_createFromFakePath(path) {
35+
_createFromFakePath(path, input) {
3636
this.lastModifiedDate = null;
3737
this.size = null;
3838
this.type = 'like/' + path.slice(path.lastIndexOf('.') + 1).toLowerCase();
3939
this.name = path.slice(path.lastIndexOf('/') + path.lastIndexOf('\\') + 2);
40+
this.input = input;
4041
}
4142
/**
4243
* Creates file like object from object
@@ -48,6 +49,7 @@ export default function __identity() {
4849
this.size = object.size;
4950
this.type = object.type;
5051
this.name = object.name;
52+
this.input = object.input;
5153
}
5254
}
5355
}

src/services/FileUploader.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)