1/* 2 * jQuery File Upload Plugin 3 * https://github.com/blueimp/jQuery-File-Upload 4 * 5 * Copyright 2010, Sebastian Tschan 6 * https://blueimp.net 7 * 8 * Licensed under the MIT license: 9 * https://opensource.org/licenses/MIT 10 */ 11 12/* jshint nomen:false */ 13/* global define, require, window, document, location, Blob, FormData */ 14 15;(function (factory) { 16 'use strict'; 17 if (typeof define === 'function' && define.amd) { 18 // Register as an anonymous AMD module: 19 define([ 20 'jquery', 21 'jquery-ui/ui/widget' 22 ], factory); 23 } else if (typeof exports === 'object') { 24 // Node/CommonJS: 25 factory( 26 require('jquery'), 27 require('./vendor/jquery.ui.widget') 28 ); 29 } else { 30 // Browser globals: 31 factory(window.jQuery); 32 } 33}(function ($) { 34 'use strict'; 35 36 // Detect file input support, based on 37 // http://viljamis.com/blog/2012/file-upload-support-on-mobile/ 38 $.support.fileInput = !(new RegExp( 39 // Handle devices which give false positives for the feature detection: 40 '(Android (1\\.[0156]|2\\.[01]))' + 41 '|(Windows Phone (OS 7|8\\.0))|(XBLWP)|(ZuneWP)|(WPDesktop)' + 42 '|(w(eb)?OSBrowser)|(webOS)' + 43 '|(Kindle/(1\\.0|2\\.[05]|3\\.0))' 44 ).test(window.navigator.userAgent) || 45 // Feature detection for all other devices: 46 $('<input type="file"/>').prop('disabled')); 47 48 // The FileReader API is not actually used, but works as feature detection, 49 // as some Safari versions (5?) support XHR file uploads via the FormData API, 50 // but not non-multipart XHR file uploads. 51 // window.XMLHttpRequestUpload is not available on IE10, so we check for 52 // window.ProgressEvent instead to detect XHR2 file upload capability: 53 $.support.xhrFileUpload = !!(window.ProgressEvent && window.FileReader); 54 $.support.xhrFormDataFileUpload = !!window.FormData; 55 56 // Detect support for Blob slicing (required for chunked uploads): 57 $.support.blobSlice = window.Blob && (Blob.prototype.slice || 58 Blob.prototype.webkitSlice || Blob.prototype.mozSlice); 59 60 // Helper function to create drag handlers for dragover/dragenter/dragleave: 61 function getDragHandler(type) { 62 var isDragOver = type === 'dragover'; 63 return function (e) { 64 e.dataTransfer = e.originalEvent && e.originalEvent.dataTransfer; 65 var dataTransfer = e.dataTransfer; 66 if (dataTransfer && $.inArray('Files', dataTransfer.types) !== -1 && 67 this._trigger( 68 type, 69 $.Event(type, {delegatedEvent: e}) 70 ) !== false) { 71 e.preventDefault(); 72 if (isDragOver) { 73 dataTransfer.dropEffect = 'copy'; 74 } 75 } 76 }; 77 } 78 79 // The fileupload widget listens for change events on file input fields defined 80 // via fileInput setting and paste or drop events of the given dropZone. 81 // In addition to the default jQuery Widget methods, the fileupload widget 82 // exposes the "add" and "send" methods, to add or directly send files using 83 // the fileupload API. 84 // By default, files added via file input selection, paste, drag & drop or 85 // "add" method are uploaded immediately, but it is possible to override 86 // the "add" callback option to queue file uploads. 87 $.widget('blueimp.fileupload', { 88 89 options: { 90 // The drop target element(s), by the default the complete document. 91 // Set to null to disable drag & drop support: 92 dropZone: $(document), 93 // The paste target element(s), by the default undefined. 94 // Set to a DOM node or jQuery object to enable file pasting: 95 pasteZone: undefined, 96 // The file input field(s), that are listened to for change events. 97 // If undefined, it is set to the file input fields inside 98 // of the widget element on plugin initialization. 99 // Set to null to disable the change listener. 100 fileInput: undefined, 101 // By default, the file input field is replaced with a clone after 102 // each input field change event. This is required for iframe transport 103 // queues and allows change events to be fired for the same file 104 // selection, but can be disabled by setting the following option to fal
104se: 105 replaceFileInput: true, 106 // The parameter name for the file form data (the request argument name). 107 // If undefined or empty, the name property of the file input field is 108 // used, or "files[]" if the file input name property is also empty, 109 // can be a string or an array of strings: 110 paramName: undefined, 111 // By default, each file of a selection is uploaded using an individual 112 // request for XHR type uploads. Set to false to upload file 113 // selections in one request each: 114 singleFileUploads: true, 115 // To limit the number of files uploaded with one XHR request, 116 // set the following option to an integer greater than 0: 117 limitMultiFileUploads: undefined, 118 // The following option limits the number of files uploaded with one 119 // XHR request to keep the request size under or equal to the defined 120 // limit in bytes: 121 limitMultiFileUploadSize: undefined, 122 // Multipart file uploads add a number of bytes to each uploaded file, 123 // therefore the following option adds an overhead for each file used 124 // in the limitMultiFileUploadSize configuration: 125 limitMultiFileUploadSizeOverhead: 512, 126 // Set the following option to true to issue all file upload requests 127 // in a sequential order: 128 sequentialUploads: false, 129 // To limit the number of concurrent uploads, 130 // set the following option to an integer greater than 0: 131 limitConcurrentUploads: undefined, 132 // Set the following option to true to force iframe transport uploads: 133 forceIframeTransport: false, 134 // Set the following option to the location of a redirect url on the 135 // origin server, for cross-domain iframe transport uploads: 136 redirect: undefined, 137 // The parameter name for the redirect url, sent as part of the form 138 // data and set to 'redirect' if this option is empty: 139 redirectParamName: undefined, 140 // Set the following option to the location of a postMessage window, 141 // to enable postMessage transport uploads: 142 postMessage: undefined, 143 // By default, XHR file uploads are sent as multipart/form-data. 144 // The iframe transport is always using multipart/form-data. 145 // Set to false to enable non-multipart XHR uploads: 146 multipart: true, 147 // To upload large files in smaller chunks, set the following option 148 // to a preferred maximum chunk size. If set to 0, null or undefined, 149 // or the browser does not support the required Blob API, files will 150 // be uploaded as a whole. 151 maxChunkSize: undefined, 152 // When a non-multipart upload or a chunked multipart upload has been 153 // aborted, this option can be used to resume the upload by setting 154 // it to the size of the already uploaded bytes. This option is most 155 // useful when modifying the options object inside of the "add" or 156 // "send" callbacks, as the options are cloned for each file upload. 157 uploadedBytes: undefined, 158 // By default, failed (abort or error) file uploads are removed from the 159 // global progress calculation. Set the following option to false to 160 // prevent recalculating the global progress data: 161 recalculateProgress: true, 162 // Interval in milliseconds to calculate and trigger progress events: 163 progressInterval: 100, 164 // Interval in milliseconds to calculate progress bitrate: 165 bitrateInterval: 500, 166 // By default, uploads are started automatically when adding files: 167 autoUpload: true, 168 // By default, duplicate file names are expected to be handled on 169 // the server-side. If this is not possible (e.g. when uploading 170 // files directly to Amazon S3), the following option can be set to 171 // an empty object or an object mapping existing filenames, e.g.: 172 // { "image.jpg": true, "image (1).jpg": true } 173 // If it is set, all files will be uploaded with unique filenames, 174 // adding increasing number suffixes if necessary, e.g.: 175 // "image (2).jpg" 176 uniqueFilenames: undefined, 177 178 // Error and info messages: 179 messages: { 180 uploadedBytes: 'Uploaded bytes exceed file size' 181 }, 182
183 // Translation function, gets the message key to be translated 184 // and an object with context specific data as arguments: 185 i18n: function (message, context) { 186 message = this.messages[message] || message.toString(); 187 if (context) { 188 $.each(context, function (key, value) { 189 message = message.replace('{' + key + '}', value); 190 }); 191 } 192 return message; 193 }, 194 195 // Additional form data to be sent along with the file uploads can be set 196 // using this option, which accepts an array of objects with name and 197 // value properties, a function returning such an array, a FormData 198 // object (for XHR file uploads), or a simple object. 199 // The form of the first fileInput is given as parameter to the function: 200 formData: function (form) { 201 return form.serializeArray(); 202 }, 203 204 // The add callback is invoked as soon as files are added to the fileupload 205 // widget (via file input selection, drag & drop, paste or add API call). 206 // If the singleFileUploads option is enabled, this callback will be 207 // called once for each file in the selection for XHR file uploads, else 208 // once for each file selection. 209 // 210 // The upload starts when the submit method is invoked on the data parameter. 211 // The data object contains a files property holding the added files 212 // and allows you to override plugin options as well as define ajax settings. 213 // 214 // Listeners for this callback can also be bound the following way: 215 // .bind('fileuploadadd', func); 216 // 217 // data.submit() returns a Promise object and allows to attach additional 218 // handlers using jQuery's Deferred callbacks: 219 // data.submit().done(func).fail(func).always(func); 220 add: function (e, data) { 221 if (e.isDefaultPrevented()) { 222 return false; 223 } 224 if (data.autoUpload || (data.autoUpload !== false && 225 $(this).fileupload('option', 'autoUpload'))) { 226 data.process().done(function () { 227 data.submit(); 228 }); 229 } 230 }, 231 232 // Other callbacks: 233 234 // Callback for the submit event of each file upload: 235 // submit: function (e, data) {}, // .bind('fileuploadsubmit', func); 236 237 // Callback for the start of each file upload request: 238 // send: function (e, data) {}, // .bind('fileuploadsend', func); 239 240 // Callback for successful uploads: 241 // done: function (e, data) {}, // .bind('fileuploaddone', func); 242 243 // Callback for failed (abort or error) uploads: 244 // fail: function (e, data) {}, // .bind('fileuploadfail', func); 245 246 // Callback for completed (success, abort or error) requests: 247 // always: function (e, data) {}, // .bind('fileuploadalways', func); 248 249 // Callback for upload progress events: 250 // progress: function (e, data) {}, // .bind('fileuploadprogress', func); 251 252 // Callback for global upload progress events: 253 // progressall: function (e, data) {}, // .bind('fileuploadprogressall', func); 254 255 // Callback for uploads start, equivalent to the global ajaxStart event: 256 // start: function (e) {}, // .bind('fileuploadstart', func); 257 258 // Callback for uploads stop, equivalent to the global ajaxStop event: 259 // stop: function (e) {}, // .bind('fileuploadstop', func); 260 261 // Callback for change events of the fileInput(s): 262 // change: function (e, data) {}, // .bind('fileuploadchange', func); 263 264 // Callback for paste events to the pasteZone(s): 265 // paste: function (e, data) {}, // .bind('fileuploadpaste', func); 266 267 // Callback for drop events of the dropZone(s): 268 // drop: function (e, data) {}, // .bind('fileuploaddrop', func); 269 270 // Callback for dragover events of the dropZone(s): 271 // dragover: function (e) {}, // .bind('fileuploaddragover', func); 272 273 // Callback before the start of each chunk upload request (before form data initialization): 274 // chunkbeforesend: function (e, data) {}, // .bind('fileuploadchunkbeforesend', func); 275 276 // Callback for the start of each chunk upload request: 277 // chunksend: function (e, data) {}, // .bind('fileuploadchunksend', func); 278 279 // Callback for successful chunk uploads: 280 // chunkdone: function (e, data) {}, // .bind('fileuploadchunkdone', func); 281 282 // Callback for failed (abort or error) chunk uploads: 283 // chunkfail: function (e, data) {}, // .bind('fileuploadchunkfail', func); 284 285 // Callback for completed (success, abort or error) chunk upload requests: 286 // chunkalways: function (e, data) {}, // .bind('fileuploadchunkalways', func); 287 288 // The plugin options are used as settings object for the ajax calls. 289 // The following are jQuery ajax settings required for the file uploads: 290 processData: false, 291 contentType: false, 292 cache: false, 293 timeout: 0 294 }, 295 296 // A list of options that require reinitializing event listeners and/or 297 // special initialization code: 298 _specialOptions: [ 299 'fileInput', 300 'dropZone', 301 'pasteZone', 302 'multipart', 303 'forceIframeTransport' 304 ], 305 306 _blobSlice: $.support.blobSlice && function () { 307 var slice = this.slice || this.webkitSlice || this.mozSlice; 308 return slice.apply(this, arguments); 309 }, 310 311 _BitrateTimer: function () { 312 this.timestamp = ((Date.now) ? Date.now() : (new Date()).getTime()); 313 this.loaded = 0; 314 this.bitrate = 0; 315 this.getBitrate = function (now, loaded, interval) { 316 var timeDiff = now - this.timestamp; 317 if (!this.bitrate || !interval || timeDiff > interval) { 318 this.bitrate = (loaded - this.loaded) * (1000 / timeDiff) * 8; 319 this.loaded = loaded; 320 this.timestamp = now; 321 } 322 return this.bitrate; 323 }; 324 }, 325 326 _isXHRUpload: function (options) { 327 return !options.forceIframeTransport && 328 ((!options.multipart && $.support.xhrFileUpload) || 329 $.support.xhrFormDataFileUpload); 330 }, 331 332 _getFormData: function (options) { 333 var formData; 334 if ($.type(options.formData) === 'function') { 335 return options.formData(options.form); 336 } 337 if ($.isArray(options.formData)) { 338 return options.formData; 339 } 340 if ($.type(options.formData) === 'object') { 341 formData = []; 342 $.each(options.formData, function (name, value) { 343 formData.push({name: name, value: value}); 344 }); 345 return formData; 346 } 347 return []; 348 }, 349 350 _getTotal: function (files) { 351 var total = 0; 352 $.each(files, function (index, file) { 353 total += file.size || 1; 354 }); 355 return total; 356 }, 357 358 _initProgressObject: function (obj) { 359 var progress = { 360 loaded: 0, 361 total: 0, 362 bitrate: 0 363 }; 364 if (obj._progress) { 365 $.extend(obj._progress, progress); 366 } else { 367 obj._progress = progress; 368 } 369 }, 370 371 _initResponseObject: function (obj) { 372 var prop; 373 if (obj._response) { 374 for (prop in obj._response) { 375 if (obj._response.hasOwnProperty(prop)) { 376 delete obj._response[prop]; 377 } 378 } 379 } else { 380 obj._response = {}; 381 } 382 }, 383 384 _onProgress: function (e, data) { 385 if (e.lengthComputable) { 386 var now = ((Date.now) ? Date.now() : (new Date()).getTime()), 387 loaded; 388 if (data._time && data.progressInterval && 389 (now - data._time < data.progressInterval) && 390 e.loaded !== e.total) { 391 return; 392 } 393 data._time = now; 394 loaded = Math.floor( 395 e.loaded / e.total * (data.chunkSize || data._progress.total) 396 ) + (data.uploadedBytes || 0); 397 // Add the difference from the previously loaded state 398 // to the global loaded counter: 399 this._progress.loaded += (loaded - data._progress.loaded); 400 this._progress.bitrate = this._bitrateTimer.getBitrate( 401 now, 402 this._progress.loaded, 403 data.bitrateInterval 404 ); 405 data._progress.loaded = data.loaded = loaded; 406 data._progress.bitrate = data.bitrate = data._bitrateTimer.getBitrate( 407 now, 408 loaded, 409 data.bitrateInterval 410 ); 411 // Trigger a custom progress event with a total data property set 412 // to the file size(s) of the current upload and a loaded data 413 // property calculated accordingly: 414 this._trigger( 415 'progress', 416 $.Event('progress', {delegatedEvent: e}), 417 data 418 ); 419 // Trigger a global progress event for all current file uploads, 420 // including ajax calls queued for sequential file uploads: 421 this._trigger( 422 'progressall', 423 $.Event('progressall', {delegatedEvent: e}), 424 this._progress 425 ); 426 } 427 }, 428 429 _initProgressListener: function (options) { 430 var that = this, 431 xhr = options.xhr ? options.xhr() : $.ajaxSettings.xhr(); 432 // Accesss to the native XHR object is required to add event listeners 433 // for the upload progress event: 434 if (xhr.upload) { 435 $(xhr.upload).bind('progress', function (e) { 436 var oe = e.originalEvent; 437 // Make sure the progress event properties get copied over: 438 e.lengthComputable = oe.lengthComputable; 439 e.loaded = oe.loaded; 440 e.total = oe.total; 441 that._onProgress(e, options); 442 }); 443 options.xhr = function () { 444 return xhr; 445 }; 446 } 447 }, 448 449 _deinitProgressListener: function (options) { 450 var xhr = options.xhr ? options.xhr() : $.ajaxSettings.xhr(); 451 if (xhr.upload) { 452 $(xhr.upload).unbind('progress'); 453 } 454 }, 455 456 _isInstanceOf: function (type, obj) { 457 // Cross-frame instanceof check 458 return Object.prototype.toString.call(obj) === '[object ' + type + ']'; 459 }, 460 461 _getUniqueFilename: function (name, map) { 462 name = String(name); 463 if (map[name]) { 464 name = name.replace( 465 /(?: \(([\d]+)\))?(\.[^.]+)?$/, 466 function (_, p1, p2) { 467 var index = p1 ? Number(p1) + 1 : 1; 468 var ext = p2 || ''; 469 return ' (' + index + ')' + ext; 470 } 471 ); 472 return this._getUniqueFilename(name, map); 473 } 474 map[name] = true; 475 return name; 476 }, 477 478 _initXHRData: function (options) { 479 var that = this, 480 formData, 481 file = options.files[0], 482 // Ignore non-multipart setting if not supported:
483 multipart = options.multipart || !$.support.xhrFileUpload, 484 paramName = $.type(options.paramName) === 'array' ? 485 options.paramName[0] : options.paramName; 486 options.headers = $.extend({}, options.headers); 487 if (options.contentRange) { 488 options.headers['Content-Range'] = options.contentRange; 489 } 490 if (!multipart || options.blob || !this._isInstanceOf('File', file)) { 491 options.headers['Content-Disposition'] = 'attachment; filename="' + 492 encodeURI(file.uploadName || file.name) + '"'; 493 } 494 if (!multipart) { 495 options.contentType = file.type || 'application/octet-stream'; 496 options.data = options.blob || file; 497 } else if ($.support.xhrFormDataFileUpload) { 498 if (options.postMessage) { 499 // window.postMessage does not allow sending FormData 500 // objects, so we just add the File/Blob objects to 501 // the formData array and let the postMessage window 502 // create the FormData object out of this array: 503 formData = this._getFormData(options); 504 if (options.blob) { 505 formData.push({ 506 name: paramName, 507 value: options.blob 508 }); 509 } else { 510 $.each(options.files, function (index, file) { 511 formData.push({ 512 name: ($.type(options.paramName) === 'array' && 513 options.paramName[index]) || paramName, 514 value: file 515 }); 516 }); 517 } 518 } else { 519 if (that._isInstanceOf('FormData', options.formData)) { 520 formData = options.formData; 521 } else { 522 formData = new FormData(); 523 $.each(this._getFormData(options), function (index, field) { 524 formData.append(field.name, field.value); 525 }); 526 } 527 if (options.blob) { 528 formData.append( 529 paramName, 530 options.blob, 531 file.uploadName || file.name 532 ); 533 } else { 534 $.each(options.files, function (index, file) { 535 // This check allows the tests to run with 536 // dummy objects: 537 if (that._isInstanceOf('File', file) || 538 that._isInstanceOf('Blob', file)) { 539 var fileName = file.uploadName || file.name; 540 if (options.uniqueFilenames) { 541 fileName = that._getUniqueFilename( 542 fileName, 543 options.uniqueFilenames 544 ); 545 } 546 formData.append( 547 ($.type(options.paramName) === 'array' && 548 options.paramName[index]) || paramName, 549 file, 550 fileName 551 ); 552 } 553 }); 554 } 555 } 556 options.data = formData; 557 } 558 // Blob reference is not needed anymore, free memory: 559 options.blob = null; 560 }, 561 562 _initIframeSettings: function (options) { 563 var targetHost = $('<a></a>').prop('href', options.url).prop('host'); 564 // Setting the dataType to iframe enables the iframe transport: 565 options.dataType = 'iframe ' + (options.dataType || ''); 566 // The iframe transport accepts a serialized array as form data: 567 options.formData = this._getFormData(options); 568 // Add redirect url to form data on cross-domain uploads: 569 if (options.redirect && targetHost && targetHost !== location.host) { 570 options.formData.push({ 571 name: options.redirectParamName || 'redirect', 572 value: options.redirect 573 }); 574 } 575 }, 576 577 _initDataSettings: function (options) { 578 if (this._isXHRUpload(options)) { 579 if (!this._chunkedUpload(options, true)) { 580 if (!options.data) { 581 this._initXHRData(options); 582 } 583 this._initProgressListener(options); 584 } 585 if (options.postMessage) { 586 // Setting the dataType to postmessage enables the 587 // postMessage transport: 588 options.dataType = 'postmessage ' + (options.dataType || ''); 589 } 590 } else { 591 this._initIframeSettings(options); 592 } 593 }, 594 595 _getParamName: function (options) { 596 var fileInput = $(options.fileInput), 597 paramName = options.paramName; 598 if (!paramName) { 599 paramName = []; 600 fileInput.each(function () { 601 var input = $(this), 602 name = input.prop('name') || 'files[]', 603 i = (input.prop('files') || [1]).length; 604 while (i) { 605 paramName.push(name); 606 i -= 1; 607 } 608 }); 609 if (!paramName.length) { 610 paramName = [fileInput.prop('name') || 'files[]']; 611 } 612 } else if (!$.isArray(paramName)) { 613 paramName = [paramName]; 614 } 615 return paramName; 616 }, 617 618 _initFormSettings: function (options) { 619 // Retrieve missing options from the input field and the 620 // associated form, if available: 621 if (!options.form || !options.form.length) { 622 options.form = $(options.fileInput.prop('form')); 623 // If the given file input doesn't have an associated form, 624 // use the default widget file input's form: 625 if (!options.form.length) { 626 options.form = $(this.options.fileInput.prop('form')); 627 } 628 } 629 options.paramName = this._getParamName(options); 630 if (!options.url) { 631 options.url = options.form.prop('action') || location.href; 632 } 633 // The HTTP request method must be "POST" or "PUT": 634 options.type = (options.type || 635 ($.type(options.form.prop('method')) === 'string' && 636 options.form.prop('method')) || '' 637 ).toUpperCase(); 638 if (options.type !== 'POST' && options.type !== 'PUT' && 639 options.type !== 'PATCH') { 640 options.type = 'POST'; 641 } 642 if (!options.formAcceptCharset) { 643 options.formAcceptCharset = options.form.attr('accept-charset'); 644 } 645 }, 646 647 _getAJAXSettings: function (data) { 648 var options = $.extend({}, this.options, data); 649 this._initFormSettings(options); 650 this._initDataSettings(options); 651 return options; 652 }, 653 654 // jQuery 1.6 doesn't provide .state(), 655 // while jQuery 1.8+ removed .isRejected() and .isResolved(): 656 _getDeferredState: function (deferred) { 657 if (deferred.state) { 658 return deferred.state(); 659 } 660 if (deferred.isResolved()) { 661 return 'resolved'; 662 } 663 if (deferred.isRejected()) { 664 return 'rejected'; 665 } 666 return 'pending'; 667 }, 668 669 // Maps jqXHR callbacks to the equivalent 670 // methods of the given Promise object: 671 _enhancePromise: function (promise) { 672 promise.success = promise.done; 673 promise.error = promise.fail; 674 promise.complete = promise.always; 675 return promise; 676 }, 677 678 // Creates and returns a Promise object enhanced with 679 // the jqXHR methods abort, success, error and complete: 680 _getXHRPromise: function (resolveOrReject, context, args
680) { 681 var dfd = $.Deferred(), 682 promise = dfd.promise(); 683 context = context || this.options.context || promise; 684 if (resolveOrReject === true) { 685 dfd.resolveWith(context, args); 686 } else if (resolveOrReject === false) { 687 dfd.rejectWith(context, args); 688 } 689 promise.abort = dfd.promise; 690 return this._enhancePromise(promise); 691 }, 692 693 // Adds convenience methods to the data callback argument: 694 _addConvenienceMethods: function (e, data) { 695 var that = this, 696 getPromise = function (args) { 697 return $.Deferred().resolveWith(that, args).promise(); 698 }; 699 data.process = function (resolveFunc, rejectFunc) { 700 if (resolveFunc || rejectFunc) { 701 data._processQueue = this._processQueue = 702 (this._processQueue || getPromise([this])).then( 703 function () { 704 if (data.errorThrown) { 705 return $.Deferred() 706 .rejectWith(that, [data]).promise(); 707 } 708 return getPromise(arguments); 709 } 710 ).then(resolveFunc, rejectFunc); 711 } 712 return this._processQueue || getPromise([this]); 713 }; 714 data.submit = function () { 715 if (this.state() !== 'pending') { 716 data.jqXHR = this.jqXHR = 717 (that._trigger( 718 'submit', 719 $.Event('submit', {delegatedEvent: e}), 720 this 721 ) !== false) && that._onSend(e, this); 722 } 723 return this.jqXHR || that._getXHRPromise(); 724 }; 725 data.abort = function () { 726 if (this.jqXHR) { 727 return this.jqXHR.abort(); 728 } 729 this.errorThrown = 'abort'; 730 that._trigger('fail', null, this); 731 return that._getXHRPromise(false); 732 }; 733 data.state = function () { 734 if (this.jqXHR) { 735 return that._getDeferredState(this.jqXHR); 736 } 737 if (this._processQueue) { 738 return that._getDeferredState(this._processQueue); 739 } 740 }; 741 data.processing = function () { 742 return !this.jqXHR && this._processQueue && that 743 ._getDeferredState(this._processQueue) === 'pending'; 744 }; 745 data.progress = function () { 746 return this._progress; 747 }; 748 data.response = function () { 749 return this._response; 750 }; 751 }, 752 753 // Parses the Range header from the server response 754 // and returns the uploaded bytes: 755 _getUploadedBytes: function (jqXHR) { 756 var range = jqXHR.getResponseHeader('Range'), 757 parts = range && range.split('-'), 758 upperBytesPos = parts && parts.length > 1 && 759 parseInt(parts[1], 10); 760 return upperBytesPos && upperBytesPos + 1; 761 }, 762 763 // Uploads a file in multiple, sequential requests 764 // by splitting the file up in multiple blob chunks. 765 // If the second parameter is true, only tests if the file 766 // should be uploaded in chunks, but does not invoke any 767 // upload requests: 768 _chunkedUpload: function (options, testOnly) { 769 options.uploadedBytes = options.uploadedBytes || 0; 770 var that = this, 771 file = options.files[0], 772 fs = file.size, 773 ub = options.uploadedBytes, 774 mcs = options.maxChunkSize || fs, 775 slice = this._blobSlice, 776 dfd = $.Deferred(), 777 promise = dfd.promise(), 778 jqXHR, 779 upload; 780 if (!(this._isXHRUpload(options) && slice && (ub || ($.type(mcs) === 'function' ? mcs(options) : mcs) < fs)) || 781 options.data) { 782 return false;
783 } 784 if (testOnly) { 785 return true; 786 } 787 if (ub >= fs) { 788 file.error = options.i18n('uploadedBytes'); 789 return this._getXHRPromise( 790 false, 791 options.context, 792 [null, 'error', file.error] 793 ); 794 } 795 // The chunk upload method: 796 upload = function () { 797 // Clone the options object for each chunk upload: 798 var o = $.extend({}, options), 799 currentLoaded = o._progress.loaded; 800 o.blob = slice.call( 801 file, 802 ub, 803 ub + ($.type(mcs) === 'function' ? mcs(o) : mcs), 804 file.type 805 ); 806 // Store the current chunk size, as the blob itself 807 // will be dereferenced after data processing: 808 o.chunkSize = o.blob.size; 809 // Expose the chunk bytes position range: 810 o.contentRange = 'bytes ' + ub + '-' + 811 (ub + o.chunkSize - 1) + '/' + fs; 812 // Trigger chunkbeforesend to allow form data to be updated for this chunk 813 that._trigger('chunkbeforesend', null, o); 814 // Process the upload data (the blob and potential form data): 815 that._initXHRData(o); 816 // Add progress listeners for this chunk upload: 817 that._initProgressListener(o); 818 jqXHR = ((that._trigger('chunksend', null, o) !== false && $.ajax(o)) || 819 that._getXHRPromise(false, o.context)) 820 .done(function (result, textStatus, jqXHR) { 821 ub = that._getUploadedBytes(jqXHR) || 822 (ub + o.chunkSize); 823 // Create a progress event if no final progress event 824 // with loaded equaling total has been triggered 825 // for this chunk: 826 if (currentLoaded + o.chunkSize - o._progress.loaded) { 827 that._onProgress($.Event('progress', { 828 lengthComputable: true, 829 loaded: ub - o.uploadedBytes, 830 total: ub - o.uploadedBytes 831 }), o); 832 } 833 options.uploadedBytes = o.uploadedBytes = ub; 834 o.result = result; 835 o.textStatus = textStatus; 836 o.jqXHR = jqXHR; 837 that._trigger('chunkdone', null, o); 838 that._trigger('chunkalways', null, o); 839 if (ub < fs) { 840 // File upload not yet complete, 841 // continue with the next chunk: 842 upload(); 843 } else { 844 dfd.resolveWith( 845 o.context, 846 [result, textStatus, jqXHR] 847 ); 848 } 849 }) 850 .fail(function (jqXHR, textStatus, errorThrown) { 851 o.jqXHR = jqXHR; 852 o.textStatus = textStatus; 853 o.errorThrown = errorThrown; 854 that._trigger('chunkfail', null, o); 855 that._trigger('chunkalways', null, o); 856 dfd.rejectWith( 857 o.context, 858 [jqXHR, textStatus, errorThrown] 859 ); 860 }) 861 .always(function () { 862 that._deinitProgressListener(o); 863 }); 864 }; 865 this._enhancePromise(promise); 866 promise.abort = function () { 867 return jqXHR.abort(); 868 }; 869 upload(); 870 return promise; 871 }, 872 873 _beforeSend: function (e, data) { 874 if (this._active === 0) { 875 // the start callback is triggered when an upload starts 876 // and no other uploads are currently running, 877 // equivalent to the global ajaxStart event: 878 this._trigger('start'); 879 // Set timer for global bitrate progress calculation: 880 this._bitrateTimer = new this._BitrateTimer(); 881 // Reset the global progress values: 882 this._progress.loaded = this._progress.total = 0; 883 this._progress.bitrate = 0; 884 } 885 // Make sure the container objects for the .response() and 886 // .progress() methods on the data object are available 887 // and reset to their initial state: 888 this._initResponseObject(data); 889 this._initProgressObject(data); 890 data._progress.loaded = data.loaded = data.uploadedBytes || 0; 891 data._progress.total = data.total = this._getTotal(data.files) || 1; 892 data._progress.bitrate = data.bitrate = 0; 893 this._active += 1; 894 // Initialize the global progress values: 895 this._progress.loaded += data.loaded; 896 this._progress.total += data.total; 897 }, 898 899 _onDone: function (result, textStatus, jqXHR, options) { 900 var total = options._progress.total, 901 response = options._response; 902 if (options._progress.loaded < total) { 903 // Create a progress event if no final progress event 904 // with loaded equaling total has been triggered: 905 this._onProgress($.Event('progress', { 906 lengthComputable: true, 907 loaded: total, 908 total: total 909 }), options); 910 } 911 response.result = options.result = result; 912 response.textStatus = options.textStatus = textStatus; 913 response.jqXHR = options.jqXHR = jqXHR; 914 this._trigger('done', null, options); 915 }, 916 917 _onFail: function (jqXHR, textStatus, errorThrown, options) { 918 var response = options._response; 919 if (options.recalculateProgress) { 920 // Remove the failed (error or abort) file upload from 921 // the global progress calculation: 922 this._progress.loaded -= options._progress.loaded;
923 this._progress.total -= options._progress.total; 924 } 925 response.jqXHR = options.jqXHR = jqXHR; 926 response.textStatus = options.textStatus = textStatus; 927 response.errorThrown = options.errorThrown = errorThrown; 928 this._trigger('fail', null, options); 929 }, 930 931 _onAlways: function (jqXHRorResult, textStatus, jqXHRorError, options) { 932 // jqXHRorResult, textStatus and jqXHRorError are added to the 933 // options object via done and fail callbacks 934 this._trigger('always', null, options); 935 }, 936 937 _onSend: function (e, data) { 938 if (!data.submit) { 939 this._addConvenienceMethods(e, data); 940 } 941 var that = this, 942 jqXHR, 943 aborted, 944 slot, 945 pipe, 946 options = that._getAJAXSettings(data), 947 send = function () { 948 that._sending += 1; 949 // Set timer for bitrate progress calculation: 950 options._bitrateTimer = new that._BitrateTimer(); 951 jqXHR = jqXHR || ( 952 ((aborted || that._trigger( 953 'send', 954 $.Event('send', {delegatedEvent: e}), 955 options 956 ) === false) && 957 that._getXHRPromise(false, options.context, aborted)) || 958 that._chunkedUpload(options) || $.ajax(options) 959 ).done(function (result, textStatus, jqXHR) { 960 that._onDone(result, textStatus, jqXHR, options); 961 }).fail(function (jqXHR, textStatus, errorThrown) { 962 that._onFail(jqXHR, textStatus, errorThrown, options); 963 }).always(function (jqXHRorResult, textStatus, jqXHRorError) { 964 that._deinitProgressListener(options); 965 that._onAlways( 966 jqXHRorResult, 967 textStatus, 968 jqXHRorError, 969 options 970 ); 971 that._sending -= 1; 972 that._active -= 1; 973 if (options.limitConcurrentUploads && 974 options.limitConcurrentUploads > that._sending) { 975 // Start the next queued upload, 976 // that has not been aborted: 977 var nextSlot = that._slots.shift(); 978 while (nextSlot) { 979 if (that._getDeferredState(nextSlot) === 'pending') { 980 nextSlot.resolve(); 981 break; 982 } 983 nextSlot = that._slots.shift(); 984 } 985 } 986 if (that._active === 0) { 987 // The stop callback is triggered when all uploads have 988 // been completed, equivalent to the global ajaxStop event: 989 that._trigger('stop'); 990 } 991 }); 992 return jqXHR; 993 }; 994 this._beforeSend(e, options); 995 if (this.options.sequentialUploads || 996 (this.options.limitConcurrentUploads && 997 this.options.limitConcurrentUploads <= this._sending)) { 998 if (this.options.limitConcurrentUploads > 1) { 999 slot = $.Deferred(); 1000 this._slots.push(slot); 1001 pipe = slot.then(send); 1002 } else { 1003 this._sequence = this._sequence.then(send, send); 1004 pipe = this._sequence; 1005 } 1006 // Return the piped Promise object, enhanced with an abort method, 1007 // which is delegated to the jqXHR object of the current upload, 1008 // and jqXHR callbacks mapped to the equivalent Promise methods: 1009 pipe.abort = function () { 1010 aborted = [undefined, 'abort', 'abort']; 1011 if (!jqXHR) { 1012 if (slot) { 1013 slot.rejectWith(options.context, aborted); 1014 } 1015 return send(); 1016 } 1017 return jqXHR.abort(); 1018 }; 1019 return this._enhancePromise(pipe); 1020 } 1021 return send(); 1022 }, 1023 1024 _onAdd: function (e, data) { 1025 var that = this, 1026 result = true, 1027 options = $.extend({}, this.options, data), 1028 files = data.files, 1029 filesLength = files.length, 1030 limit = options.limitMultiFileUploads, 1031 limitSize = options.limitMultiFileUploadSize, 1032 overhead = options.limitMultiFileUploadSizeOverhead, 1033 batchSize = 0, 1034 paramName = this._getParamName(options), 1035 paramNameSet, 1036 paramNameSlice, 1037 fileSet, 1038 i, 1039 j = 0; 1040 if (!filesLength) { 1041 return false;
1042 } 1043 if (limitSize && files[0].size === undefined) { 1044 limitSize = undefined; 1045 } 1046 if (!(options.singleFileUploads || limit || limitSize) || 1047 !this._isXHRUpload(options)) { 1048 fileSet = [files]; 1049 paramNameSet = [paramName]; 1050 } else if (!(options.singleFileUploads || limitSize) && limit) { 1051 fileSet = []; 1052 paramNameSet = []; 1053 for (i = 0; i < filesLength; i += limit) { 1054 fileSet.push(files.slice(i, i + limit)); 1055 paramNameSlice = paramName.slice(i, i + limit); 1056 if (!paramNameSlice.length) { 1057 paramNameSlice = paramName; 1058 } 1059 paramNameSet.push(paramNameSlice); 1060 } 1061 } else if (!options.singleFileUploads && limitSize) { 1062 fileSet = []; 1063 paramNameSet = []; 1064 for (i = 0; i < filesLength; i = i + 1) { 1065 batchSize += files[i].size + overhead; 1066 if (i + 1 === filesLength || 1067 ((batchSize + files[i + 1].size + overhead) > limitSize) || 1068 (limit && i + 1 - j >= limit)) { 1069 fileSet.push(files.slice(j, i + 1)); 1070 paramNameSlice = paramName.slice(j, i + 1); 1071 if (!paramNameSlice.length) { 1072 paramNameSlice = paramName; 1073 } 1074 paramNameSet.push(paramNameSlice); 1075 j = i + 1; 1076 batchSize = 0; 1077 } 1078 } 1079 } else { 1080 paramNameSet = paramName; 1081 } 1082 data.originalFiles = files; 1083 $.each(fileSet || files, function (index, element) { 1084 var newData = $.extend({}, data); 1085 newData.files = fileSet ? element : [element]; 1086 newData.paramName = paramNameSet[index]; 1087 that._initResponseObject(newData); 1088 that._initProgressObject(newData); 1089 that._addConvenienceMethods(e, newData); 1090 result = that._trigger( 1091 'add', 1092 $.Event('add', {delegatedEvent: e}), 1093 newData 1094 ); 1095 return result; 1096 }); 1097 return result; 1098 }, 1099 1100 _replaceFileInput: function (data) { 1101 var input = data.fileInput, 1102 inputClone = input.clone(true), 1103 restoreFocus = input.is(document.activeElement); 1104 // Add a reference for the new cloned file input to the data argument: 1105 data.fileInputClone = inputClone; 1106 $('<form></form>').append(inputClone)[0].reset(); 1107 // Detaching allows to insert the fileInput on another form 1108 // without loosing the file input value: 1109 input.after(inputClone).detach(); 1110 // If the fileInput had focus before it was detached, 1111 // restore focus to the inputClone. 1112 if (restoreFocus) { 1113 inputClone.focus(); 1114 } 1115 // Avoid memory leaks with the detached file input: 1116 $.cleanData(input.unbind('remove')); 1117 // Replace the original file input element in the fileInput 1118 // elements set with the clone, which has been copied including 1119 // event handlers: 1120 this.options.fileInput = this.options.fileInput.map(function (i, el) { 1121 if (el === input[0]) { 1122 return inputClone[0]; 1123 } 1124 return el; 1125 }); 1126 // If the widget has been initialized on the file input itself, 1127 // override this.element with the file input clone: 1128 if (input[0] === this.element[0]) { 1129 this.element = inputClone; 1130 } 1131 }, 1132 1133 _handleFileTreeEntry: function (entry, path) { 1134 var that = this, 1135 dfd = $.Deferred(),
1136 entries = [], 1137 dirReader, 1138 errorHandler = function (e) { 1139 if (e && !e.entry) { 1140 e.entry = entry; 1141 } 1142 // Since $.when returns immediately if one 1143 // Deferred is rejected, we use resolve instead. 1144 // This allows valid files and invalid items 1145 // to be returned together in one set: 1146 dfd.resolve([e]); 1147 }, 1148 successHandler = function (entries) { 1149 that._handleFileTreeEntries( 1150 entries, 1151 path + entry.name + '/' 1152 ).done(function (files) { 1153 dfd.resolve(files); 1154 }).fail(errorHandler); 1155 }, 1156 readEntries = function () { 1157 dirReader.readEntries(function (results) { 1158 if (!results.length) { 1159 successHandler(entries); 1160 } else { 1161 entries = entries.concat(results); 1162 readEntries(); 1163 } 1164 }, errorHandler); 1165 }; 1166 path = path || ''; 1167 if (entry.isFile) { 1168 if (entry._file) { 1169 // Workaround for Chrome bug #149735 1170 entry._file.relativePath = path; 1171 dfd.resolve(entry._file); 1172 } else { 1173 entry.file(function (file) { 1174 file.relativePath = path; 1175 dfd.resolve(file); 1176 }, errorHandler); 1177 } 1178 } else if (entry.isDirectory) { 1179 dirReader = entry.createReader(); 1180 readEntries(); 1181 } else { 1182 // Return an empty list for file system items 1183 // other than files or directories: 1184 dfd.resolve([]); 1185 } 1186 return dfd.promise(); 1187 }, 1188 1189 _handleFileTreeEntries: function (entries, path) { 1190 var that = this; 1191 return $.when.apply( 1192 $, 1193 $.map(entries, function (entry) { 1194 return that._handleFileTreeEntry(entry, path); 1195 }) 1196 ).then(function () { 1197 return Array.prototype.concat.apply( 1198 [], 1199 arguments 1200 ); 1201 }); 1202 }, 1203 1204 _getDroppedFiles: function (dataTransfer) { 1205 dataTransfer = dataTransfer || {}; 1206 var items = dataTransfer.items; 1207 if (items && items.length && (items[0].webkitGetAsEntry || 1208 items[0].getAsEntry)) { 1209 return this._handleFileTreeEntries( 1210 $.map(items, function (item) { 1211 var entry; 1212 if (item.webkitGetAsEntry) { 1213 entry = item.webkitGetAsEntry(); 1214 if (entry) { 1215 // Workaround for Chrome bug #149735: 1216 entry._file = item.getAsFile(); 1217 } 1218 return entry; 1219 } 1220 return item.getAsEntry(); 1221 }) 1222 ); 1223 } 1224 return $.Deferred().resolve( 1225 $.makeArray(dataTransfer.files) 1226 ).promise(); 1227 }, 1228 1229 _getSingleFileInputFiles: function (fileInput) { 1230 fileInput = $(fileInput); 1231 var entries = fileInput.prop('webkitEntries') || 1232 fileInput.prop('entries'), 1233 files, 1234 value; 1235 if (entries && entries.length) { 1236 return this._handleFileTreeEntries(entries); 1237 } 1238 files = $.makeArray(fileInput.prop('files')); 1239 if (!files.length) { 1240 value = fileInput.prop('value'); 1241 if (!value) { 1242 return $.Deferred().resolve([]).promise(); 1243 } 1244 // If the files property is not available, the browser does not 1245 // support the File API and we add a pseudo File object with 1246 // the input value as name with path information removed: 1247 files = [{name: value.replace(/^.*\\/, '')}]; 1248 } else if (files[0].name === undefined && files[0].fileName) { 1249 // File normalization for Safari 4 and Firefox 3: 1250 $.each(files, function (index, file) { 1251 file.name = file.fileName; 1252 file.size = file.fileSize; 1253 }); 1254 } 1255 return $.Deferred().resolve(files).promise(); 1256 }, 1257 1258 _getFileInputFiles: function (fileInput) { 1259 if (!(fileInput instanceof $) || fileInput.length === 1) { 1260 return this._getSingleFileInputFiles(fileInput); 1261 } 1262 return $.when.apply( 1263 $, 1264 $.map(fileInput, this._getSingleFileInputFiles) 1265 ).then(function () { 1266 return Array.prototype.concat.apply( 1267 [], 1268 arguments 1269 ); 1270 }); 1271 }, 1272 1273 _onChange: function (e) { 1274 var that = this, 1275 data = { 1276 fileInput: $(e.target), 1277 form: $(e.target.form) 1278 }; 1279 this._getFileInputFiles(data.fileInput).always(function (files) { 1280 data.files = files; 1281 if (that.options.replaceFileInput) { 1282 that._replaceFileInput(data); 1283 } 1284 if (that._trigger( 1285 'change', 1286 $.Event('change', {delegatedEvent: e}), 1287 data 1288 ) !== false) { 1289 that._onAdd(e, data); 1290 } 1291 }); 1292 }, 1293 1294 _onPaste: function (e) { 1295 var items = e.originalEvent && e.originalEvent.clipboardData && 1296 e.originalEvent.clipboardData.items, 1297 data = {files: []}; 1298 if (items && items.length) { 1299 $.each(items, function (index, item) { 1300 var file = item.getAsFile && item.getAsFile(); 1301 if (file) { 1302 data.files.push(file); 1303 } 1304 }); 1305 if (this._trigger( 1306 'paste', 1307 $.Event('paste', {delegatedEvent: e}), 1308 data 1309 ) !== false) { 1310 this._onAdd(e, data); 1311 } 1312 } 1313 }, 1314 1315 _onDrop: function (e) { 1316 e.dataTransfer = e.originalEvent && e.originalEvent.dataTransfer; 1317 var that = this, 1318 dataTransfer = e.dataTransfer, 1319 data = {}; 1320 if (dataTransfer && dataTransfer.files && dataTransfer.files.length) { 1321 e.preventDefault(); 1322 this._getDroppedFiles(dataTransfer).always(function (files) { 1323 data.files = files; 1324 if (that._trigger( 1325 'drop', 1326 $.Event('drop', {delegatedEvent: e}), 1327 data 1328 ) !== false) { 1329 that._onAdd(e, data); 1330 } 1331 }); 1332 } 1333 }, 1334 1335 _onDragOver: getDragHandler('dragover'), 1336 1337 _onDragEnter: getDragHandler('dragenter'), 1338 1339 _onDragLeave: getDragHandler('dragleave'), 1340 1341 _initEventHandlers: function () { 1342 if (this._isXHRUpload(this.options)) { 1343 this._on(this.options.dropZone, { 1344 dragover: this._onDragOver, 1345 drop: this._onDrop, 1346 // event.preventDefault() on dragenter is required for IE10+: 1347 dragenter: this._onDragEnter, 1348 // dragleave is not required, but added for c
1348ompleteness: 1349 dragleave: this._onDragLeave 1350 }); 1351 this._on(this.options.pasteZone, { 1352 paste: this._onPaste 1353 }); 1354 } 1355 if ($.support.fileInput) { 1356 this._on(this.options.fileInput, { 1357 change: this._onChange 1358 }); 1359 } 1360 }, 1361 1362 _destroyEventHandlers: function () { 1363 this._off(this.options.dropZone, 'dragenter dragleave dragover drop'); 1364 this._off(this.options.pasteZone, 'paste'); 1365 this._off(this.options.fileInput, 'change'); 1366 }, 1367 1368 _destroy: function () { 1369 this._destroyEventHandlers(); 1370 }, 1371 1372 _setOption: function (key, value) { 1373 var reinit = $.inArray(key, this._specialOptions) !== -1; 1374 if (reinit) { 1375 this._destroyEventHandlers(); 1376 } 1377 this._super(key, value); 1378 if (reinit) { 1379 this._initSpecialOptions(); 1380 this._initEventHandlers(); 1381 } 1382 }, 1383 1384 _initSpecialOptions: function () { 1385 var options = this.options; 1386 if (options.fileInput === undefined) { 1387 options.fileInput = this.element.is('input[type="file"]') ? 1388 this.element : this.element.find('input[type="file"]'); 1389 } else if (!(options.fileInput instanceof $)) { 1390 options.fileInput = $(options.fileInput); 1391 } 1392 if (!(options.dropZone instanceof $)) { 1393 options.dropZone = $(options.dropZone); 1394 } 1395 if (!(options.pasteZone instanceof $)) { 1396 options.pasteZone = $(options.pasteZone); 1397 } 1398 }, 1399 1400 _getRegExp: function (str) { 1401 var parts = str.split('/'), 1402 modifiers = parts.pop(); 1403 parts.shift(); 1404 return new RegExp(parts.join('/'), modifiers); 1405 }, 1406 1407 _isRegExpOption: function (key, value) { 1408 return key !== 'url' && $.type(value) === 'string' && 1409 /^\/.*\/[igm]{0,3}$/.test(value); 1410 }, 1411 1412 _initDataAttributes: function () { 1413 var that = this, 1414 options = this.options, 1415 data = this.element.data(); 1416 // Initialize options set via HTML5 data-attributes: 1417 $.each( 1418 this.element[0].attributes, 1419 function (index, attr) { 1420 var key = attr.name.toLowerCase(), 1421 value; 1422 if (/^data-/.test(key)) { 1423 // Convert hyphen-ated key to camelCase: 1424 key = key.slice(5).replace(/-[a-z]/g, function (str) { 1425 return str.charAt(1).toUpperCase(); 1426 }); 1427 value = data[key]; 1428 if (that._isRegExpOption(key, value)) { 1429 value = that._getRegExp(value); 1430 } 1431 options[key] = value; 1432 } 1433 } 1434 ); 1435 }, 1436 1437 _create: function () { 1438 this._initDataAttributes(); 1439 this._initSpecialOptions(); 1440 this._slots = []; 1441 this._sequence = this._getXHRPromise(true); 1442 this._sending = this._active = 0; 1443 this._initProgressObject(this); 1444 this._initEventHandlers(); 1445 }, 1446 1447 // This method is exposed to the widget API and allows to query 1448 // the number of active uploads: 1449 active: function () { 1450 return this._active; 1451 }, 1452 1453 // This method is exposed to the widget API and allows to query 1454 // the widget upload progress. 1455 // It returns an object with loaded, total and bitrate properties 1456 // for the running uploads: 1457 progress: function () { 1458 return this._progress; 1459 }, 1460 1461 // This method is exposed to the widget API and allows adding files 1462 // using the fileupload API. The data parameter accepts an object which 1463 // must have a files property and can contain additional options: 1464 // .fileupload('add', {files: filesList}); 1465 add: function (data) { 1466 var that = this; 1467 if (!data || this.options.disabled) { 1468 return; 1469 } 1470 if (data.fileInput && !data.files) { 1471 this._getFileInputFiles(data.fileInput).always(function (files) { 1472 data.files = files; 1473 that._onAdd(null, data); 1474 }); 1475 } else { 1476 data.files = $.makeArray(data.files); 1477 this._onAdd(null, data); 1478 } 1479 }, 1480 1481 // This method is exposed to the widget API and allows sending files 1482 // using the fileupload API. The data parameter accepts an object which 1483 // must have a files or fileInput property and can contain additional options: 1484 // .fileupload('send', {files: filesList}); 1485 // The method returns a Promise object for the file upload call. 1486 send: function (data) { 1487 if (data && !this.options.disabled) { 1488 if (data.fileInput && !data.files) { 1489 var that = this, 1490 dfd = $.Deferred(), 1491 promise = dfd.promise(), 1492 jqXHR, 1493 aborted; 1494 promise.abort = function () { 1495 aborted = true; 1496 if (jqXHR) { 1497 return jqXHR.abort(); 1498 } 1499 dfd.reject(null, 'abort', 'abort'); 1500 return promise; 1501 }; 1502 this._getFileInputFiles(data.fileInput).always( 1503 function (files) { 1504 if (aborted) { 1505 return; 1506 } 1507 if (!files.length) { 1508 dfd.reject(); 1509 return; 1510 } 1511 data.files = files; 1512 jqXHR = that._onSend(null, data); 1513 jqXHR.then( 1514 function (result, textStatus, jqXHR) { 1515 dfd.resolve(result, textStatus, jqXHR); 1516 }, 1517 function (jqXHR, textStatus, errorThrown) { 1518 dfd.reject(jqXHR, textStatus, errorThrown); 1519 } 1520 ); 1521 } 1522 ); 1523 return this._enhancePromise(promise); 1524 } 1525 data.files = $.makeArray(data.files); 1526 if (data.files.length) { 1527 return this._onSend(null, data); 1528 } 1529 } 1530 return this._getXHRPromise(false, data && data.context); 1531 } 1532 1533 }); 1534 1535}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.