Skip to content

Commit 3de48c6

Browse files
committed
Allow using an array of strings as paramName option.
Make use of the name properties of all given file input fields as paramName option if it is not set.
1 parent a596c7e commit 3de48c6

File tree

3 files changed

+65
-20
lines changed

3 files changed

+65
-20
lines changed

js/jquery.fileupload.js

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload Plugin 5.9
2+
* jQuery File Upload Plugin 5.10.0
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -63,7 +63,8 @@
6363
replaceFileInput: true,
6464
// The parameter name for the file form data (the request argument name).
6565
// 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:
6768
paramName: undefined,
6869
// By default, each file of a selection is uploaded using an individual
6970
// request for XHR type uploads. Set to false to upload file
@@ -258,7 +259,8 @@
258259
var formData,
259260
file = options.files[0],
260261
// Ignore non-multipart setting if not supported:
261-
multipart = options.multipart || !$.support.xhrFileUpload;
262+
multipart = options.multipart || !$.support.xhrFileUpload,
263+
paramName = options.paramName[0];
262264
if (!multipart || options.blob) {
263265
// For non-multipart uploads and chunked uploads,
264266
// file meta data is not part of the request body,
@@ -290,13 +292,13 @@
290292
formData = this._getFormData(options);
291293
if (options.blob) {
292294
formData.push({
293-
name: options.paramName,
295+
name: paramName,
294296
value: options.blob
295297
});
296298
} else {
297299
$.each(options.files, function (index, file) {
298300
formData.push({
299-
name: options.paramName,
301+
name: options.paramName[index] || paramName,
300302
value: file
301303
});
302304
});
@@ -311,14 +313,18 @@
311313
});
312314
}
313315
if (options.blob) {
314-
formData.append(options.paramName, options.blob, file.name);
316+
formData.append(paramName, options.blob, file.name);
315317
} else {
316318
$.each(options.files, function (index, file) {
317319
// File objects are also Blob instances.
318320
// This check allows the tests to run with
319321
// dummy objects:
320322
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+
);
322328
}
323329
});
324330
}
@@ -362,16 +368,36 @@
362368
}
363369
},
364370

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+
365394
_initFormSettings: function (options) {
366395
// Retrieve missing options from the input field and the
367396
// associated form, if available:
368397
if (!options.form || !options.form.length) {
369398
options.form = $(options.fileInput.prop('form'));
370399
}
371-
if (!options.paramName) {
372-
options.paramName = options.fileInput.prop('name') ||
373-
'files[]';
374-
}
400+
options.paramName = this._getParamName(options);
375401
if (!options.url) {
376402
options.url = options.form.prop('action') || location.href;
377403
}
@@ -634,21 +660,34 @@
634660
result = true,
635661
options = $.extend({}, this.options, data),
636662
limit = options.limitMultiFileUploads,
663+
paramName = this._getParamName(options),
664+
paramNameSet,
665+
paramNameSlice,
637666
fileSet,
638667
i;
639668
if (!(options.singleFileUploads || limit) ||
640669
!this._isXHRUpload(options)) {
641670
fileSet = [data.files];
671+
paramNameSet = [paramName];
642672
} else if (!options.singleFileUploads && limit) {
643673
fileSet = [];
674+
paramNameSet = [];
644675
for (i = 0; i < data.files.length; i += limit) {
645676
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);
646682
}
683+
} else {
684+
paramNameSet = paramName;
647685
}
648686
data.originalFiles = data.files;
649687
$.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];
652691
newData.submit = function () {
653692
newData.jqXHR = this.jqXHR =
654693
(that._trigger('submit', e, this) !== false) &&

js/jquery.iframe-transport.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery Iframe Transport Plugin 1.3
2+
* jQuery Iframe Transport Plugin 1.4
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2011, Sebastian Tschan
@@ -30,7 +30,8 @@
3030
// The iframe transport accepts three additional options:
3131
// options.fileInput: a jQuery collection of file input fields
3232
// options.paramName: the parameter name for the file form data,
33-
// overrides the name property of the file input field(s)
33+
// overrides the name property of the file input field(s),
34+
// can be a string or an array of strings.
3435
// options.formData: an array of objects with name and value properties,
3536
// equivalent to the return data of .serializeArray(), e.g.:
3637
// [{name: 'a', value: 1}, {name: 'b', value: 2}]
@@ -50,7 +51,9 @@
5051
'<iframe src="javascript:false;" name="iframe-transport-' +
5152
(counter += 1) + '"></iframe>'
5253
).bind('load', function () {
53-
var fileInputClones;
54+
var fileInputClones,
55+
paramNames = $.isArray(options.paramName) ?
56+
options.paramName : [options.paramName];
5457
iframe
5558
.unbind('load')
5659
.bind('load', function () {
@@ -101,8 +104,11 @@
101104
return fileInputClones[index];
102105
});
103106
if (options.paramName) {
104-
options.fileInput.each(function () {
105-
$(this).prop('name', options.paramName);
107+
options.fileInput.each(function (index) {
108+
$(this).prop(
109+
'name',
110+
paramNames[index] || options.paramName
111+
);
106112
});
107113
}
108114
// Appending the file input fields to the hidden form

test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* jQuery File Upload Plugin Test 6.5
2+
* jQuery File Upload Plugin Test 6.6
33
* https://github.com/blueimp/jQuery-File-Upload
44
*
55
* Copyright 2010, Sebastian Tschan
@@ -552,7 +552,7 @@ $(function () {
552552
paramName: null,
553553
send: function (e, data) {
554554
strictEqual(
555-
data.paramName,
555+
data.paramName[0],
556556
data.fileInput.prop('name'),
557557
'Takes paramName from file input field if not set'
558558
);

0 commit comments

Comments
 (0)