2 * jQuery File Upload Plugin 5.11.2
3 * https://github.com/blueimp/jQuery-File-Upload
5 * Copyright 2010, Sebastian Tschan
8 * Licensed under the MIT license:
9 * http://www.opensource.org/licenses/MIT
12 /*jslint nomen: true, unparam: true, regexp: true */
13 /*global define, window, document, Blob, FormData, location */
17 if (typeof define === 'function' && define.amd) {
18 // Register as an anonymous AMD module:
25 factory(window.jQuery);
30 // The FileReader API is not actually used, but works as feature detection,
31 // as e.g. Safari supports XHR file uploads via the FormData API,
32 // but not non-multipart XHR file uploads:
33 $.support.xhrFileUpload = !!(window.XMLHttpRequestUpload && window.FileReader);
34 $.support.xhrFormDataFileUpload = !!window.FormData;
36 // The fileupload widget listens for change events on file input fields defined
37 // via fileInput setting and paste or drop events of the given dropZone.
38 // In addition to the default jQuery Widget methods, the fileupload widget
39 // exposes the "add" and "send" methods, to add or directly send files using
40 // the fileupload API.
41 // By default, files added via file input selection, paste, drag & drop or
42 // "add" method are uploaded immediately, but it is possible to override
43 // the "add" callback option to queue file uploads.
44 $.widget('blueimp.fileupload', {
47 // The namespace used for event handler binding on the dropZone and
48 // fileInput collections.
49 // If not set, the name of the widget ("fileupload") is used.
51 // The drop target collection, by the default the complete document.
52 // Set to null or an empty collection to disable drag & drop support:
53 dropZone: $(document),
54 // The file input field collection, that is listened for change events.
55 // If undefined, it is set to the file input fields inside
56 // of the widget element on plugin initialization.
57 // Set to null or an empty collection to disable the change listener.
59 // By default, the file input field is replaced with a clone after
60 // each input field change event. This is required for iframe transport
61 // queues and allows change events to be fired for the same file
62 // selection, but can be disabled by setting the following option to false:
63 replaceFileInput: true,
64 // The parameter name for the file form data (the request argument name).
65 // If undefined or empty, the name property of the file input field is
66 // used, or "files[]" if the file input name property is also empty,
67 // can be a string or an array of strings:
69 // By default, each file of a selection is uploaded using an individual
70 // request for XHR type uploads. Set to false to upload file
71 // selections in one request each:
72 singleFileUploads: true,
73 // To limit the number of files uploaded with one XHR request,
74 // set the following option to an integer greater than 0:
75 limitMultiFileUploads: undefined,
76 // Set the following option to true to issue all file upload requests
77 // in a sequential order:
78 sequentialUploads: false,
79 // To limit the number of concurrent uploads,
80 // set the following option to an integer greater than 0:
81 limitConcurrentUploads: undefined,
82 // Set the following option to true to force iframe transport uploads:
83 forceIframeTransport: false,
84 // Set the following option to the location of a redirect url on the
85 // origin server, for cross-domain iframe transport uploads:
87 // The parameter name for the redirect url, sent as part of the form
88 // data and set to 'redirect' if this option is empty:
89 redirectParamName: undefined,
90 // Set the following option to the location of a postMessage window,
91 // to enable postMessage transport uploads:
92 postMessage: undefined,
93 // By default, XHR file uploads are sent as multipart/form-data.
94 // The iframe transport is always using multipart/form-data.
95 // Set to false to enable non-multipart XHR uploads:
97 // To upload large files in smaller chunks, set the following option
98 // to a preferred maximum chunk size. If set to 0, null or undefined,
99 // or the browser does not support the required Blob API, files will
100 // be uploaded as a whole.
101 maxChunkSize: undefined,
102 // When a non-multipart upload or a chunked multipart upload has been
103 // aborted, this option can be used to resume the upload by setting
104 // it to the size of the already uploaded bytes. This option is most
105 // useful when modifying the options object inside of the "add" or
106 // "send" callbacks, as the options are cloned for each file upload.
107 uploadedBytes: undefined,
108 // By default, failed (abort or error) file uploads are removed from the
109 // global progress calculation. Set the following option to false to
110 // prevent recalculating the global progress data:
111 recalculateProgress: true,
112 // Interval in milliseconds to calculate and trigger progress events:
113 progressInterval: 100,
114 // Interval in milliseconds to calculate progress bitrate:
115 bitrateInterval: 500,
117 // Additional form data to be sent along with the file uploads can be set
118 // using this option, which accepts an array of objects with name and
119 // value properties, a function returning such an array, a FormData
120 // object (for XHR file uploads), or a simple object.
121 // The form of the first fileInput is given as parameter to the function:
122 formData: function (form) {
123 return form.serializeArray();
126 // The add callback is invoked as soon as files are added to the fileupload
127 // widget (via file input selection, drag & drop, paste or add API call).
128 // If the singleFileUploads option is enabled, this callback will be
129 // called once for each file in the selection for XHR file uplaods, else
130 // once for each file selection.
131 // The upload starts when the submit method is invoked on the data parameter.
132 // The data object contains a files property holding the added files
133 // and allows to override plugin options as well as define ajax settings.
134 // Listeners for this callback can also be bound the following way:
135 // .bind('fileuploadadd', func);
136 // data.submit() returns a Promise object and allows to attach additional
137 // handlers using jQuery's Deferred callbacks:
138 // data.submit().done(func).fail(func).always(func);
139 add: function (e, data) {
144 // Callback for the submit event of each file upload:
145 // submit: function (e, data) {}, // .bind('fileuploadsubmit', func);
146 // Callback for the start of each file upload request:
147 // send: function (e, data) {}, // .bind('fileuploadsend', func);
148 // Callback for successful uploads:
149 // done: function (e, data) {}, // .bind('fileuploaddone', func);
150 // Callback for failed (abort or error) uploads:
151 // fail: function (e, data) {}, // .bind('fileuploadfail', func);
152 // Callback for completed (success, abort or error) requests:
153 // always: function (e, data) {}, // .bind('fileuploadalways', func);
154 // Callback for upload progress events:
155 // progress: function (e, data) {}, // .bind('fileuploadprogress', func);
156 // Callback for global upload progress events:
157 // progressall: function (e, data) {}, // .bind('fileuploadprogressall', func);
158 // Callback for uploads start, equivalent to the global ajaxStart event:
159 // start: function (e) {}, // .bind('fileuploadstart', func);
160 // Callback for uploads stop, equivalent to the global ajaxStop event:
161 // stop: function (e) {}, // .bind('fileuploadstop', func);
162 // Callback for change events of the fileInput collection:
163 // change: function (e, data) {}, // .bind('fileuploadchange', func);
164 // Callback for paste events to the dropZone collection:
165 // paste: function (e, data) {}, // .bind('fileuploadpaste', func);
166 // Callback for drop events of the dropZone collection:
167 // drop: function (e, data) {}, // .bind('fileuploaddrop', func);
168 // Callback for dragover events of the dropZone collection:
169 // dragover: function (e) {}, // .bind('fileuploaddragover', func);
171 // The plugin options are used as settings object for the ajax calls.
172 // The following are jQuery ajax settings required for the file uploads:
178 // A list of options that require a refresh after assigning a new value:
179 _refreshOptionsList: [
184 'forceIframeTransport'
187 _BitrateTimer: function () {
188 this.timestamp = +(new Date());
191 this.getBitrate = function (now, loaded, interval) {
192 var timeDiff = now - this.timestamp;
193 if (!this.bitrate || !interval || timeDiff > interval) {
194 this.bitrate = (loaded - this.loaded) * (1000 / timeDiff) * 8;
195 this.loaded = loaded;
196 this.timestamp = now;
202 _isXHRUpload: function (options) {
203 return !options.forceIframeTransport &&
204 ((!options.multipart && $.support.xhrFileUpload) ||
205 $.support.xhrFormDataFileUpload);
208 _getFormData: function (options) {
210 if (typeof options.formData === 'function') {
211 return options.formData(options.form);
213 if ($.isArray(options.formData)) {
214 return options.formData;
216 if (options.formData) {
218 $.each(options.formData, function (name, value) {
219 formData.push({name: name, value: value});
226 _getTotal: function (files) {
228 $.each(files, function (index, file) {
229 total += file.size || 1;
234 _onProgress: function (e, data) {
235 if (e.lengthComputable) {
236 var now = +(new Date()),
239 if (data._time && data.progressInterval &&
240 (now - data._time < data.progressInterval) &&
241 e.loaded !== e.total) {
245 total = data.total || this._getTotal(data.files);
247 e.loaded / e.total * (data.chunkSize || total),
249 ) + (data.uploadedBytes || 0);
250 this._loaded += loaded - (data.loaded || data.uploadedBytes || 0);
251 data.lengthComputable = true;
252 data.loaded = loaded;
254 data.bitrate = data._bitrateTimer.getBitrate(
259 // Trigger a custom progress event with a total data property set
260 // to the file size(s) of the current upload and a loaded data
261 // property calculated accordingly:
262 this._trigger('progress', e, data);
263 // Trigger a global progress event for all current file uploads,
264 // including ajax calls queued for sequential file uploads:
265 this._trigger('progressall', e, {
266 lengthComputable: true,
267 loaded: this._loaded,
269 bitrate: this._bitrateTimer.getBitrate(
278 _initProgressListener: function (options) {
280 xhr = options.xhr ? options.xhr() : $.ajaxSettings.xhr();
281 // Accesss to the native XHR object is required to add event listeners
282 // for the upload progress event:
284 $(xhr.upload).bind('progress', function (e) {
285 var oe = e.originalEvent;
286 // Make sure the progress event properties get copied over:
287 e.lengthComputable = oe.lengthComputable;
288 e.loaded = oe.loaded;
290 that._onProgress(e, options);
292 options.xhr = function () {
298 _initXHRData: function (options) {
300 file = options.files[0],
301 // Ignore non-multipart setting if not supported:
302 multipart = options.multipart || !$.support.xhrFileUpload,
303 paramName = options.paramName[0];
304 if (!multipart || options.blob) {
305 // For non-multipart uploads and chunked uploads,
306 // file meta data is not part of the request body,
307 // so we transmit this data as part of the HTTP headers.
308 // For cross domain requests, these headers must be allowed
309 // via Access-Control-Allow-Headers or removed using
310 // the beforeSend callback:
311 options.headers = $.extend(options.headers, {
312 'X-File-Name': file.name,
313 'X-File-Type': file.type,
314 'X-File-Size': file.size
317 // Non-chunked non-multipart upload:
318 options.contentType = file.type;
320 } else if (!multipart) {
321 // Chunked non-multipart upload:
322 options.contentType = 'application/octet-stream';
323 options.data = options.blob;
326 if (multipart && $.support.xhrFormDataFileUpload) {
327 if (options.postMessage) {
328 // window.postMessage does not allow sending FormData
329 // objects, so we just add the File/Blob objects to
330 // the formData array and let the postMessage window
331 // create the FormData object out of this array:
332 formData = this._getFormData(options);
339 $.each(options.files, function (index, file) {
341 name: options.paramName[index] || paramName,
347 if (options.formData instanceof FormData) {
348 formData = options.formData;
350 formData = new FormData();
351 $.each(this._getFormData(options), function (index, field) {
352 formData.append(field.name, field.value);
356 formData.append(paramName, options.blob, file.name);
358 $.each(options.files, function (index, file) {
359 // File objects are also Blob instances.
360 // This check allows the tests to run with
362 if (file instanceof Blob) {
364 options.paramName[index] || paramName,
372 options.data = formData;
374 // Blob reference is not needed anymore, free memory:
378 _initIframeSettings: function (options) {
379 // Setting the dataType to iframe enables the iframe transport:
380 options.dataType = 'iframe ' + (options.dataType || '');
381 // The iframe transport accepts a serialized array as form data:
382 options.formData = this._getFormData(options);
383 // Add redirect url to form data on cross-domain uploads:
384 if (options.redirect && $('<a></a>').prop('href', options.url)
385 .prop('host') !== location.host) {
386 options.formData.push({
387 name: options.redirectParamName || 'redirect',
388 value: options.redirect
393 _initDataSettings: function (options) {
394 if (this._isXHRUpload(options)) {
395 if (!this._chunkedUpload(options, true)) {
397 this._initXHRData(options);
399 this._initProgressListener(options);
401 if (options.postMessage) {
402 // Setting the dataType to postmessage enables the
403 // postMessage transport:
404 options.dataType = 'postmessage ' + (options.dataType || '');
407 this._initIframeSettings(options, 'iframe');
411 _getParamName: function (options) {
412 var fileInput = $(options.fileInput),
413 paramName = options.paramName;
416 fileInput.each(function () {
418 name = input.prop('name') || 'files[]',
419 i = (input.prop('files') || [1]).length;
421 paramName.push(name);
425 if (!paramName.length) {
426 paramName = [fileInput.prop('name') || 'files[]'];
428 } else if (!$.isArray(paramName)) {
429 paramName = [paramName];
434 _initFormSettings: function (options) {
435 // Retrieve missing options from the input field and the
436 // associated form, if available:
437 if (!options.form || !options.form.length) {
438 options.form = $(options.fileInput.prop('form'));
440 options.paramName = this._getParamName(options);
442 options.url = options.form.prop('action') || location.href;
444 // The HTTP request method must be "POST" or "PUT":
445 options.type = (options.type || options.form.prop('method') || '')
447 if (options.type !== 'POST' && options.type !== 'PUT') {
448 options.type = 'POST';
452 _getAJAXSettings: function (data) {
453 var options = $.extend({}, this.options, data);
454 this._initFormSettings(options);
455 this._initDataSettings(options);
459 // Maps jqXHR callbacks to the equivalent
460 // methods of the given Promise object:
461 _enhancePromise: function (promise) {
462 promise.success = promise.done;
463 promise.error = promise.fail;
464 promise.complete = promise.always;
468 // Creates and returns a Promise object enhanced with
469 // the jqXHR methods abort, success, error and complete:
470 _getXHRPromise: function (resolveOrReject, context, args) {
471 var dfd = $.Deferred(),
472 promise = dfd.promise();
473 context = context || this.options.context || promise;
474 if (resolveOrReject === true) {
475 dfd.resolveWith(context, args);
476 } else if (resolveOrReject === false) {
477 dfd.rejectWith(context, args);
479 promise.abort = dfd.promise;
480 return this._enhancePromise(promise);
483 // Uploads a file in multiple, sequential requests
484 // by splitting the file up in multiple blob chunks.
485 // If the second parameter is true, only tests if the file
486 // should be uploaded in chunks, but does not invoke any
488 _chunkedUpload: function (options, testOnly) {
490 file = options.files[0],
492 ub = options.uploadedBytes = options.uploadedBytes || 0,
493 mcs = options.maxChunkSize || fs,
494 // Use the Blob methods with the slice implementation
495 // according to the W3C Blob API specification:
496 slice = file.webkitSlice || file.mozSlice || file.slice,
501 if (!(this._isXHRUpload(options) && slice && (ub || mcs < fs)) ||
509 file.error = 'uploadedBytes';
510 return this._getXHRPromise(
513 [null, 'error', file.error]
516 // n is the number of blobs to upload,
517 // calculated via filesize, uploaded bytes and max chunk size:
518 n = Math.ceil((fs - ub) / mcs);
519 // The chunk upload method accepting the chunk number as parameter:
520 upload = function (i) {
522 return that._getXHRPromise(true, options.context);
524 // Upload the blobs in sequential order:
525 return upload(i -= 1).pipe(function () {
526 // Clone the options object for each chunk upload:
527 var o = $.extend({}, options);
533 // Store the current chunk size, as the blob itself
534 // will be dereferenced after data processing:
535 o.chunkSize = o.blob.size;
536 // Process the upload data (the blob and potential form data):
537 that._initXHRData(o);
538 // Add progress listeners for this chunk upload:
539 that._initProgressListener(o);
540 jqXHR = ($.ajax(o) || that._getXHRPromise(false, o.context))
542 // Create a progress event if upload is done and
543 // no progress event has been invoked for this chunk:
545 that._onProgress($.Event('progress', {
546 lengthComputable: true,
551 options.uploadedBytes = o.uploadedBytes +=
557 // Return the piped Promise object, enhanced with an abort method,
558 // which is delegated to the jqXHR object of the current upload,
559 // and jqXHR callbacks mapped to the equivalent Promise methods:
561 pipe.abort = function () {
562 return jqXHR.abort();
564 return this._enhancePromise(pipe);
567 _beforeSend: function (e, data) {
568 if (this._active === 0) {
569 // the start callback is triggered when an upload starts
570 // and no other uploads are currently running,
571 // equivalent to the global ajaxStart event:
572 this._trigger('start');
573 // Set timer for global bitrate progress calculation:
574 this._bitrateTimer = new this._BitrateTimer();
577 // Initialize the global progress values:
578 this._loaded += data.uploadedBytes || 0;
579 this._total += this._getTotal(data.files);
582 _onDone: function (result, textStatus, jqXHR, options) {
583 if (!this._isXHRUpload(options)) {
584 // Create a progress event for each iframe load:
585 this._onProgress($.Event('progress', {
586 lengthComputable: true,
591 options.result = result;
592 options.textStatus = textStatus;
593 options.jqXHR = jqXHR;
594 this._trigger('done', null, options);
597 _onFail: function (jqXHR, textStatus, errorThrown, options) {
598 options.jqXHR = jqXHR;
599 options.textStatus = textStatus;
600 options.errorThrown = errorThrown;
601 this._trigger('fail', null, options);
602 if (options.recalculateProgress) {
603 // Remove the failed (error or abort) file upload from
604 // the global progress calculation:
605 this._loaded -= options.loaded || options.uploadedBytes || 0;
606 this._total -= options.total || this._getTotal(options.files);
610 _onAlways: function (jqXHRorResult, textStatus, jqXHRorError, options) {
612 options.textStatus = textStatus;
613 if (jqXHRorError && jqXHRorError.always) {
614 options.jqXHR = jqXHRorError;
615 options.result = jqXHRorResult;
617 options.jqXHR = jqXHRorResult;
618 options.errorThrown = jqXHRorError;
620 this._trigger('always', null, options);
621 if (this._active === 0) {
622 // The stop callback is triggered when all uploads have
623 // been completed, equivalent to the global ajaxStop event:
624 this._trigger('stop');
625 // Reset the global progress values:
626 this._loaded = this._total = 0;
627 this._bitrateTimer = null;
631 _onSend: function (e, data) {
636 options = that._getAJAXSettings(data),
637 send = function (resolve, args) {
639 // Set timer for bitrate progress calculation:
640 options._bitrateTimer = new that._BitrateTimer();
642 (resolve !== false &&
643 that._trigger('send', e, options) !== false &&
644 (that._chunkedUpload(options) || $.ajax(options))) ||
645 that._getXHRPromise(false, options.context, args)
646 ).done(function (result, textStatus, jqXHR) {
647 that._onDone(result, textStatus, jqXHR, options);
648 }).fail(function (jqXHR, textStatus, errorThrown) {
649 that._onFail(jqXHR, textStatus, errorThrown, options);
650 }).always(function (jqXHRorResult, textStatus, jqXHRorError) {
658 if (options.limitConcurrentUploads &&
659 options.limitConcurrentUploads > that._sending) {
660 // Start the next queued upload,
661 // that has not been aborted:
662 var nextSlot = that._slots.shift();
664 if (!nextSlot.isRejected()) {
668 nextSlot = that._slots.shift();
674 this._beforeSend(e, options);
675 if (this.options.sequentialUploads ||
676 (this.options.limitConcurrentUploads &&
677 this.options.limitConcurrentUploads <= this._sending)) {
678 if (this.options.limitConcurrentUploads > 1) {
680 this._slots.push(slot);
681 pipe = slot.pipe(send);
683 pipe = (this._sequence = this._sequence.pipe(send, send));
685 // Return the piped Promise object, enhanced with an abort method,
686 // which is delegated to the jqXHR object of the current upload,
687 // and jqXHR callbacks mapped to the equivalent Promise methods:
688 pipe.abort = function () {
689 var args = [undefined, 'abort', 'abort'];
692 slot.rejectWith(args);
694 return send(false, args);
696 return jqXHR.abort();
698 return this._enhancePromise(pipe);
703 _onAdd: function (e, data) {
706 options = $.extend({}, this.options, data),
707 limit = options.limitMultiFileUploads,
708 paramName = this._getParamName(options),
713 if (!(options.singleFileUploads || limit) ||
714 !this._isXHRUpload(options)) {
715 fileSet = [data.files];
716 paramNameSet = [paramName];
717 } else if (!options.singleFileUploads && limit) {
720 for (i = 0; i < data.files.length; i += limit) {
721 fileSet.push(data.files.slice(i, i + limit));
722 paramNameSlice = paramName.slice(i, i + limit);
723 if (!paramNameSlice.length) {
724 paramNameSlice = paramName;
726 paramNameSet.push(paramNameSlice);
729 paramNameSet = paramName;
731 data.originalFiles = data.files;
732 $.each(fileSet || data.files, function (index, element) {
733 var newData = $.extend({}, data);
734 newData.files = fileSet ? element : [element];
735 newData.paramName = paramNameSet[index];
736 newData.submit = function () {
737 newData.jqXHR = this.jqXHR =
738 (that._trigger('submit', e, this) !== false) &&
739 that._onSend(e, this);
742 return (result = that._trigger('add', e, newData));
747 // File Normalization for Gecko 1.9.1 (Firefox 3.5) support:
748 _normalizeFile: function (index, file) {
749 if (file.name === undefined && file.size === undefined) {
750 file.name = file.fileName;
751 file.size = file.fileSize;
755 _replaceFileInput: function (input) {
756 var inputClone = input.clone(true);
757 $('<form></form>').append(inputClone)[0].reset();
758 // Detaching allows to insert the fileInput on another form
759 // without loosing the file input value:
760 input.after(inputClone).detach();
761 // Avoid memory leaks with the detached file input:
762 $.cleanData(input.unbind('remove'));
763 // Replace the original file input element in the fileInput
764 // collection with the clone, which has been copied including
766 this.options.fileInput = this.options.fileInput.map(function (i, el) {
767 if (el === input[0]) {
768 return inputClone[0];
772 // If the widget has been initialized on the file input itself,
773 // override this.element with the file input clone:
774 if (input[0] === this.element[0]) {
775 this.element = inputClone;
779 _onChange: function (e) {
780 var that = e.data.fileupload,
782 files: $.each($.makeArray(e.target.files), that._normalizeFile),
783 fileInput: $(e.target),
784 form: $(e.target.form)
786 if (!data.files.length) {
787 // If the files property is not available, the browser does not
788 // support the File API and we add a pseudo File object with
789 // the input value as name with path information removed:
790 data.files = [{name: e.target.value.replace(/^.*\\/, '')}];
792 if (that.options.replaceFileInput) {
793 that._replaceFileInput(data.fileInput);
795 if (that._trigger('change', e, data) === false ||
796 that._onAdd(e, data) === false) {
801 _onPaste: function (e) {
802 var that = e.data.fileupload,
803 cbd = e.originalEvent.clipboardData,
804 items = (cbd && cbd.items) || [],
806 $.each(items, function (index, item) {
807 var file = item.getAsFile && item.getAsFile();
809 data.files.push(file);
812 if (that._trigger('paste', e, data) === false ||
813 that._onAdd(e, data) === false) {
818 _onDrop: function (e) {
819 var that = e.data.fileupload,
820 dataTransfer = e.dataTransfer = e.originalEvent.dataTransfer,
823 $.makeArray(dataTransfer && dataTransfer.files),
827 if (that._trigger('drop', e, data) === false ||
828 that._onAdd(e, data) === false) {
834 _onDragOver: function (e) {
835 var that = e.data.fileupload,
836 dataTransfer = e.dataTransfer = e.originalEvent.dataTransfer;
837 if (that._trigger('dragover', e) === false) {
841 dataTransfer.dropEffect = dataTransfer.effectAllowed = 'copy';
846 _initEventHandlers: function () {
847 var ns = this.options.namespace;
848 if (this._isXHRUpload(this.options)) {
849 this.options.dropZone
850 .bind('dragover.' + ns, {fileupload: this}, this._onDragOver)
851 .bind('drop.' + ns, {fileupload: this}, this._onDrop)
852 .bind('paste.' + ns, {fileupload: this}, this._onPaste);
854 this.options.fileInput
855 .bind('change.' + ns, {fileupload: this}, this._onChange);
858 _destroyEventHandlers: function () {
859 var ns = this.options.namespace;
860 this.options.dropZone
861 .unbind('dragover.' + ns, this._onDragOver)
862 .unbind('drop.' + ns, this._onDrop)
863 .unbind('paste.' + ns, this._onPaste);
864 this.options.fileInput
865 .unbind('change.' + ns, this._onChange);
868 _setOption: function (key, value) {
869 var refresh = $.inArray(key, this._refreshOptionsList) !== -1;
871 this._destroyEventHandlers();
873 $.Widget.prototype._setOption.call(this, key, value);
875 this._initSpecialOptions();
876 this._initEventHandlers();
880 _initSpecialOptions: function () {
881 var options = this.options;
882 if (options.fileInput === undefined) {
883 options.fileInput = this.element.is('input:file') ?
884 this.element : this.element.find('input:file');
885 } else if (!(options.fileInput instanceof $)) {
886 options.fileInput = $(options.fileInput);
888 if (!(options.dropZone instanceof $)) {
889 options.dropZone = $(options.dropZone);
893 _create: function () {
894 var options = this.options;
895 // Initialize options set via HTML5 data-attributes:
896 $.extend(options, $(this.element[0].cloneNode(false)).data());
897 options.namespace = options.namespace || this.widgetName;
898 this._initSpecialOptions();
900 this._sequence = this._getXHRPromise(true);
901 this._sending = this._active = this._loaded = this._total = 0;
902 this._initEventHandlers();
905 destroy: function () {
906 this._destroyEventHandlers();
907 $.Widget.prototype.destroy.call(this);
910 enable: function () {
911 $.Widget.prototype.enable.call(this);
912 this._initEventHandlers();
915 disable: function () {
916 this._destroyEventHandlers();
917 $.Widget.prototype.disable.call(this);
920 // This method is exposed to the widget API and allows adding files
921 // using the fileupload API. The data parameter accepts an object which
922 // must have a files property and can contain additional options:
923 // .fileupload('add', {files: filesList});
924 add: function (data) {
925 if (!data || this.options.disabled) {
928 data.files = $.each($.makeArray(data.files), this._normalizeFile);
929 this._onAdd(null, data);
932 // This method is exposed to the widget API and allows sending files
933 // using the fileupload API. The data parameter accepts an object which
934 // must have a files property and can contain additional options:
935 // .fileupload('send', {files: filesList});
936 // The method returns a Promise object for the file upload call.
937 send: function (data) {
938 if (data && !this.options.disabled) {
939 data.files = $.each($.makeArray(data.files), this._normalizeFile);
940 if (data.files.length) {
941 return this._onSend(null, data);
944 return this._getXHRPromise(false, data && data.context);