Skip to content

Commit d005a20

Browse files
committed
Refactored progress calculation.
Expose the progress data via .progress() method on the data argument. Add convenience methods to files uploaded via send API call.
1 parent 70a41e9 commit d005a20

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

js/jquery.fileupload.js

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload Plugin 5.25.1
2+
* jQuery File Upload Plugin 5.26
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -261,33 +261,39 @@
261261
return total;
262262
},
263263

264+
_initProgressObject: function (obj) {
265+
obj._progress = {
266+
loaded: 0,
267+
total: 0,
268+
bitrate: 0
269+
};
270+
},
271+
264272
_onProgress: function (e, data) {
265273
if (e.lengthComputable) {
266274
var now = +(new Date()),
267-
total,
268275
loaded;
269276
if (data._time && data.progressInterval &&
270277
(now - data._time < data.progressInterval) &&
271278
e.loaded !== e.total) {
272279
return;
273280
}
274281
data._time = now;
275-
total = data.total || this._getTotal(data.files);
276282
loaded = Math.floor(
277-
e.loaded / e.total * (data.chunkSize || total)
283+
e.loaded / e.total * (data.chunkSize || data._progress.total)
278284
) + (data.uploadedBytes || 0);
279-
this._progress.loaded += loaded -
280-
(data.loaded || data.uploadedBytes || 0);
281-
data.loaded = loaded;
282-
data.total = total;
283-
data.bitrate = data._bitrateTimer.getBitrate(
285+
// Add the difference from the previously loaded state
286+
// to the global loaded counter:
287+
this._progress.loaded += (loaded - data._progress.loaded);
288+
this._progress.bitrate = this._bitrateTimer.getBitrate(
284289
now,
285-
loaded,
290+
this._progress.loaded,
286291
data.bitrateInterval
287292
);
288-
this._progress.bitrate = this._bitrateTimer.getBitrate(
293+
data._progress.loaded = data.loaded = loaded;
294+
data._progress.bitrate = data.bitrate = data._bitrateTimer.getBitrate(
289295
now,
290-
this._progress.loaded,
296+
loaded,
291297
data.bitrateInterval
292298
);
293299
// Trigger a custom progress event with a total data property set
@@ -542,6 +548,9 @@
542548
return that._getDeferredState(this.jqXHR);
543549
}
544550
};
551+
data.progress = function () {
552+
return this._progress;
553+
};
545554
},
546555

547556
// Parses the Range header from the server response
@@ -588,7 +597,8 @@
588597
// The chunk upload method:
589598
upload = function () {
590599
// Clone the options object for each chunk upload:
591-
var o = $.extend({}, options);
600+
var o = $.extend({}, options),
601+
currentLoaded = o._progress.loaded;
592602
o.blob = slice.call(
593603
file,
594604
ub,
@@ -610,10 +620,10 @@
610620
.done(function (result, textStatus, jqXHR) {
611621
ub = that._getUploadedBytes(jqXHR) ||
612622
(ub + o.chunkSize);
613-
// Create a progress event if upload is done and no progress
614-
// event has been invoked for this chunk, or there has been
615-
// no progress event with loaded equaling total:
616-
if (!o.loaded || o.loaded < o.total) {
623+
// Create a progress event if no final progress event
624+
// with loaded equaling total has been triggered
625+
// for this chunk:
626+
if (o._progress.loaded === currentLoaded) {
617627
that._onProgress($.Event('progress', {
618628
lengthComputable: true,
619629
loaded: ub - o.uploadedBytes,
@@ -669,19 +679,23 @@
669679
this._progress.loaded = this._progress.total = 0;
670680
this._progress.bitrate = 0;
671681
}
682+
if (!data._progress) {
683+
data._progress = {};
684+
}
685+
data._progress.loaded = data.loaded = data.uploadedBytes || 0;
686+
data._progress.total = data.total = this._getTotal(data.files) || 1;
687+
data._progress.bitrate = data.bitrate = 0;
672688
this._active += 1;
673689
// Initialize the global progress values:
674-
this._progress.loaded += data.uploadedBytes || 0;
675-
this._progress.total += this._getTotal(data.files);
690+
this._progress.loaded += data.loaded;
691+
this._progress.total += data.total;
676692
},
677693

678694
_onDone: function (result, textStatus, jqXHR, options) {
679-
if (!options.uploadedBytes && (!this._isXHRUpload(options) ||
680-
!options.loaded || options.loaded < options.total)) {
681-
var total = this._getTotal(options.files) || 1;
682-
// Create a progress event for each iframe load,
683-
// or if there has been no progress event with
684-
// loaded equaling total for XHR uploads:
695+
var total = options._progress.total;
696+
if (options._progress.loaded < total) {
697+
// Create a progress event if no final progress event
698+
// with loaded equaling total has been triggered:
685699
this._onProgress($.Event('progress', {
686700
lengthComputable: true,
687701
loaded: total,
@@ -702,10 +716,8 @@
702716
if (options.recalculateProgress) {
703717
// Remove the failed (error or abort) file upload from
704718
// the global progress calculation:
705-
this._progress.loaded -= options.loaded ||
706-
options.uploadedBytes || 0;
707-
this._progress.total -= options.total ||
708-
this._getTotal(options.files);
719+
this._progress.loaded -= options._progress.loaded;
720+
this._progress.total -= options._progress.total;
709721
}
710722
},
711723

@@ -722,6 +734,9 @@
722734
},
723735

724736
_onSend: function (e, data) {
737+
if (!data.submit) {
738+
this._addConvenienceMethods(e, data);
739+
}
725740
var that = this,
726741
jqXHR,
727742
aborted,
@@ -826,6 +841,7 @@
826841
var newData = $.extend({}, data);
827842
newData.files = fileSet ? element : [element];
828843
newData.paramName = paramNameSet[index];
844+
that._initProgressObject(newData);
829845
that._addConvenienceMethods(e, newData);
830846
result = that._trigger('add', e, newData);
831847
return result;
@@ -1101,11 +1117,7 @@
11011117
this._slots = [];
11021118
this._sequence = this._getXHRPromise(true);
11031119
this._sending = this._active = 0;
1104-
this._progress = {
1105-
loaded: 0,
1106-
total: 0,
1107-
bitrate: 0
1108-
};
1120+
this._initProgressObject(this);
11091121
this._initEventHandlers();
11101122
},
11111123

0 commit comments

Comments
 (0)