Skip to content

Commit 2334b11

Browse files
committed
Expose global progress via .fileupload('progress') method.
1 parent 8609fa5 commit 2334b11

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

js/jquery.fileupload.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload Plugin 5.24
2+
* jQuery File Upload Plugin 5.25
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -276,31 +276,27 @@
276276
loaded = Math.floor(
277277
e.loaded / e.total * (data.chunkSize || total)
278278
) + (data.uploadedBytes || 0);
279-
this._loaded += loaded - (data.loaded || data.uploadedBytes || 0);
280-
data.lengthComputable = true;
279+
this._progress.loaded += loaded -
280+
(data.loaded || data.uploadedBytes || 0);
281281
data.loaded = loaded;
282282
data.total = total;
283283
data.bitrate = data._bitrateTimer.getBitrate(
284284
now,
285285
loaded,
286286
data.bitrateInterval
287287
);
288+
this._progress.bitrate = this._bitrateTimer.getBitrate(
289+
now,
290+
this._progress.loaded,
291+
data.bitrateInterval
292+
);
288293
// Trigger a custom progress event with a total data property set
289294
// to the file size(s) of the current upload and a loaded data
290295
// property calculated accordingly:
291296
this._trigger('progress', e, data);
292297
// Trigger a global progress event for all current file uploads,
293298
// including ajax calls queued for sequential file uploads:
294-
this._trigger('progressall', e, {
295-
lengthComputable: true,
296-
loaded: this._loaded,
297-
total: this._total,
298-
bitrate: this._bitrateTimer.getBitrate(
299-
now,
300-
this._loaded,
301-
data.bitrateInterval
302-
)
303-
});
299+
this._trigger('progressall', e, this._progress);
304300
}
305301
},
306302

@@ -645,11 +641,14 @@
645641
this._trigger('start');
646642
// Set timer for global bitrate progress calculation:
647643
this._bitrateTimer = new this._BitrateTimer();
644+
// Reset the global progress values:
645+
this._progress.loaded = this._progress.total = 0;
646+
this._progress.bitrate = 0;
648647
}
649648
this._active += 1;
650649
// Initialize the global progress values:
651-
this._loaded += data.uploadedBytes || 0;
652-
this._total += this._getTotal(data.files);
650+
this._progress.loaded += data.uploadedBytes || 0;
651+
this._progress.total += this._getTotal(data.files);
653652
},
654653

655654
_onDone: function (result, textStatus, jqXHR, options) {
@@ -679,8 +678,10 @@
679678
if (options.recalculateProgress) {
680679
// Remove the failed (error or abort) file upload from
681680
// the global progress calculation:
682-
this._loaded -= options.loaded || options.uploadedBytes || 0;
683-
this._total -= options.total || this._getTotal(options.files);
681+
this._progress.loaded -= options.loaded ||
682+
options.uploadedBytes || 0;
683+
this._progress.total -= options.total ||
684+
this._getTotal(options.files);
684685
}
685686
},
686687

@@ -693,9 +694,6 @@
693694
// The stop callback is triggered when all uploads have
694695
// been completed, equivalent to the global ajaxStop event:
695696
this._trigger('stop');
696-
// Reset the global progress values:
697-
this._loaded = this._total = 0;
698-
this._bitrateTimer = null;
699697
}
700698
},
701699

@@ -1093,10 +1091,23 @@
10931091
this._initSpecialOptions();
10941092
this._slots = [];
10951093
this._sequence = this._getXHRPromise(true);
1096-
this._sending = this._active = this._loaded = this._total = 0;
1094+
this._sending = this._active = 0;
1095+
this._progress = {
1096+
loaded: 0,
1097+
total: 0,
1098+
bitrate: 0
1099+
};
10971100
this._initEventHandlers();
10981101
},
10991102

1103+
// This method is exposed to the widget API and allows to query
1104+
// the widget upload progress.
1105+
// It returns an object with loaded, total and bitrate properties
1106+
// for the running uploads:
1107+
progress: function () {
1108+
return this._progress;
1109+
},
1110+
11001111
// This method is exposed to the widget API and allows adding files
11011112
// using the fileupload API. The data parameter accepts an object which
11021113
// must have a files property and can contain additional options:

0 commit comments

Comments
 (0)