|
1 | 1 | /* |
2 | | - * jQuery File Upload Plugin 5.9 |
| 2 | + * jQuery File Upload Plugin 5.10.0 |
3 | 3 | * https://github.com/blueimp/jQuery-File-Upload |
4 | 4 | * |
5 | 5 | * Copyright 2010, Sebastian Tschan |
|
63 | 63 | replaceFileInput: true, |
64 | 64 | // The parameter name for the file form data (the request argument name). |
65 | 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: |
| 66 | + // used, or "files[]" if the file input name property is also empty, |
| 67 | + // can be a string or an array of strings: |
67 | 68 | paramName: undefined, |
68 | 69 | // By default, each file of a selection is uploaded using an individual |
69 | 70 | // request for XHR type uploads. Set to false to upload file |
|
258 | 259 | var formData, |
259 | 260 | file = options.files[0], |
260 | 261 | // Ignore non-multipart setting if not supported: |
261 | | - multipart = options.multipart || !$.support.xhrFileUpload; |
| 262 | + multipart = options.multipart || !$.support.xhrFileUpload, |
| 263 | + paramName = options.paramName[0]; |
262 | 264 | if (!multipart || options.blob) { |
263 | 265 | // For non-multipart uploads and chunked uploads, |
264 | 266 | // file meta data is not part of the request body, |
|
290 | 292 | formData = this._getFormData(options); |
291 | 293 | if (options.blob) { |
292 | 294 | formData.push({ |
293 | | - name: options.paramName, |
| 295 | + name: paramName, |
294 | 296 | value: options.blob |
295 | 297 | }); |
296 | 298 | } else { |
297 | 299 | $.each(options.files, function (index, file) { |
298 | 300 | formData.push({ |
299 | | - name: options.paramName, |
| 301 | + name: options.paramName[index] || paramName, |
300 | 302 | value: file |
301 | 303 | }); |
302 | 304 | }); |
|
311 | 313 | }); |
312 | 314 | } |
313 | 315 | if (options.blob) { |
314 | | - formData.append(options.paramName, options.blob, file.name); |
| 316 | + formData.append(paramName, options.blob, file.name); |
315 | 317 | } else { |
316 | 318 | $.each(options.files, function (index, file) { |
317 | 319 | // File objects are also Blob instances. |
318 | 320 | // This check allows the tests to run with |
319 | 321 | // dummy objects: |
320 | 322 | if (file instanceof Blob) { |
321 | | - formData.append(options.paramName, file, file.name); |
| 323 | + formData.append( |
| 324 | + options.paramName[index] || paramName, |
| 325 | + file, |
| 326 | + file.name |
| 327 | + ); |
322 | 328 | } |
323 | 329 | }); |
324 | 330 | } |
|
362 | 368 | } |
363 | 369 | }, |
364 | 370 |
|
| 371 | + _getParamName: function (options) { |
| 372 | + var fileInput = $(options.fileInput), |
| 373 | + paramName = options.paramName; |
| 374 | + if (!paramName) { |
| 375 | + paramName = []; |
| 376 | + fileInput.each(function () { |
| 377 | + var input = $(this), |
| 378 | + name = input.prop('name') || 'files[]', |
| 379 | + i = (input.prop('files') || [1]).length; |
| 380 | + while (i) { |
| 381 | + paramName.push(name); |
| 382 | + i -= 1; |
| 383 | + } |
| 384 | + }); |
| 385 | + if (!paramName.length) { |
| 386 | + paramName = [fileInput.prop('name') || 'files[]']; |
| 387 | + } |
| 388 | + } else if (!$.isArray(paramName)) { |
| 389 | + paramName = [paramName]; |
| 390 | + } |
| 391 | + return paramName; |
| 392 | + }, |
| 393 | + |
365 | 394 | _initFormSettings: function (options) { |
366 | 395 | // Retrieve missing options from the input field and the |
367 | 396 | // associated form, if available: |
368 | 397 | if (!options.form || !options.form.length) { |
369 | 398 | options.form = $(options.fileInput.prop('form')); |
370 | 399 | } |
371 | | - if (!options.paramName) { |
372 | | - options.paramName = options.fileInput.prop('name') || |
373 | | - 'files[]'; |
374 | | - } |
| 400 | + options.paramName = this._getParamName(options); |
375 | 401 | if (!options.url) { |
376 | 402 | options.url = options.form.prop('action') || location.href; |
377 | 403 | } |
|
634 | 660 | result = true, |
635 | 661 | options = $.extend({}, this.options, data), |
636 | 662 | limit = options.limitMultiFileUploads, |
| 663 | + paramName = this._getParamName(options), |
| 664 | + paramNameSet, |
| 665 | + paramNameSlice, |
637 | 666 | fileSet, |
638 | 667 | i; |
639 | 668 | if (!(options.singleFileUploads || limit) || |
640 | 669 | !this._isXHRUpload(options)) { |
641 | 670 | fileSet = [data.files]; |
| 671 | + paramNameSet = [paramName]; |
642 | 672 | } else if (!options.singleFileUploads && limit) { |
643 | 673 | fileSet = []; |
| 674 | + paramNameSet = []; |
644 | 675 | for (i = 0; i < data.files.length; i += limit) { |
645 | 676 | fileSet.push(data.files.slice(i, i + limit)); |
| 677 | + paramNameSlice = paramName.slice(i, i + limit); |
| 678 | + if (!paramNameSlice.length) { |
| 679 | + paramNameSlice = paramName; |
| 680 | + } |
| 681 | + paramNameSet.push(paramNameSlice); |
646 | 682 | } |
| 683 | + } else { |
| 684 | + paramNameSet = paramName; |
647 | 685 | } |
648 | 686 | data.originalFiles = data.files; |
649 | 687 | $.each(fileSet || data.files, function (index, element) { |
650 | | - var files = fileSet ? element : [element], |
651 | | - newData = $.extend({}, data, {files: files}); |
| 688 | + var newData = $.extend({}, data); |
| 689 | + newData.files = fileSet ? element : [element]; |
| 690 | + newData.paramName = paramNameSet[index]; |
652 | 691 | newData.submit = function () { |
653 | 692 | newData.jqXHR = this.jqXHR = |
654 | 693 | (that._trigger('submit', e, this) !== false) && |
|
0 commit comments