Permalink
Browse files

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.
  • Loading branch information...
1 parent a596c7e commit 3de48c611ef52ece9c9b185e9e8e6fe2ba45ec76 @blueimp committed Mar 16, 2012
Showing with 65 additions and 20 deletions.
  1. +52 −13 js/jquery.fileupload.js
  2. +11 −5 js/jquery.iframe-transport.js
  3. +2 −2 test/test.js
View
@@ -1,5 +1,5 @@
/*
- * jQuery File Upload Plugin 5.9
+ * jQuery File Upload Plugin 5.10.0
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
@@ -63,7 +63,8 @@
replaceFileInput: true,
// The parameter name for the file form data (the request argument name).
// If undefined or empty, the name property of the file input field is
- // used, or "files[]" if the file input name property is also empty:
+ // used, or "files[]" if the file input name property is also empty,
+ // can be a string or an array of strings:
paramName: undefined,
// By default, each file of a selection is uploaded using an individual
// request for XHR type uploads. Set to false to upload file
@@ -258,7 +259,8 @@
var formData,
file = options.files[0],
// Ignore non-multipart setting if not supported:
- multipart = options.multipart || !$.support.xhrFileUpload;
+ multipart = options.multipart || !$.support.xhrFileUpload,
+ paramName = options.paramName[0];
if (!multipart || options.blob) {
// For non-multipart uploads and chunked uploads,
// file meta data is not part of the request body,
@@ -290,13 +292,13 @@
formData = this._getFormData(options);
if (options.blob) {
formData.push({
- name: options.paramName,
+ name: paramName,
value: options.blob
});
} else {
$.each(options.files, function (index, file) {
formData.push({
- name: options.paramName,
+ name: options.paramName[index] || paramName,
value: file
});
});
@@ -311,14 +313,18 @@
});
}
if (options.blob) {
- formData.append(options.paramName, options.blob, file.name);
+ formData.append(paramName, options.blob, file.name);
} else {
$.each(options.files, function (index, file) {
// File objects are also Blob instances.
// This check allows the tests to run with
// dummy objects:
if (file instanceof Blob) {
- formData.append(options.paramName, file, file.name);
+ formData.append(
+ options.paramName[index] || paramName,
+ file,
+ file.name
+ );
}
});
}
@@ -362,16 +368,36 @@
}
},
+ _getParamName: function (options) {
+ var fileInput = $(options.fileInput),
+ paramName = options.paramName;
+ if (!paramName) {
+ paramName = [];
+ fileInput.each(function () {
+ var input = $(this),
+ name = input.prop('name') || 'files[]',
+ i = (input.prop('files') || [1]).length;
+ while (i) {
+ paramName.push(name);
+ i -= 1;
+ }
+ });
+ if (!paramName.length) {
+ paramName = [fileInput.prop('name') || 'files[]'];
+ }
+ } else if (!$.isArray(paramName)) {
+ paramName = [paramName];
+ }
+ return paramName;
+ },
+
_initFormSettings: function (options) {
// Retrieve missing options from the input field and the
// associated form, if available:
if (!options.form || !options.form.length) {
options.form = $(options.fileInput.prop('form'));
}
- if (!options.paramName) {
- options.paramName = options.fileInput.prop('name') ||
- 'files[]';
- }
+ options.paramName = this._getParamName(options);
if (!options.url) {
options.url = options.form.prop('action') || location.href;
}
@@ -634,21 +660,34 @@
result = true,
options = $.extend({}, this.options, data),
limit = options.limitMultiFileUploads,
+ paramName = this._getParamName(options),
+ paramNameSet,
+ paramNameSlice,
fileSet,
i;
if (!(options.singleFileUploads || limit) ||
!this._isXHRUpload(options)) {
fileSet = [data.files];
+ paramNameSet = [paramName];
} else if (!options.singleFileUploads && limit) {
fileSet = [];
+ paramNameSet = [];
for (i = 0; i < data.files.length; i += limit) {
fileSet.push(data.files.slice(i, i + limit));
+ paramNameSlice = paramName.slice(i, i + limit);
+ if (!paramNameSlice.length) {
+ paramNameSlice = paramName;
+ }
+ paramNameSet.push(paramNameSlice);
}
+ } else {
+ paramNameSet = paramName;
}
data.originalFiles = data.files;
$.each(fileSet || data.files, function (index, element) {
- var files = fileSet ? element : [element],
- newData = $.extend({}, data, {files: files});
+ var newData = $.extend({}, data);
+ newData.files = fileSet ? element : [element];
+ newData.paramName = paramNameSet[index];
newData.submit = function () {
newData.jqXHR = this.jqXHR =
(that._trigger('submit', e, this) !== false) &&
@@ -1,5 +1,5 @@
/*
- * jQuery Iframe Transport Plugin 1.3
+ * jQuery Iframe Transport Plugin 1.4
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2011, Sebastian Tschan
@@ -30,7 +30,8 @@
// The iframe transport accepts three additional options:
// options.fileInput: a jQuery collection of file input fields
// options.paramName: the parameter name for the file form data,
- // overrides the name property of the file input field(s)
+ // overrides the name property of the file input field(s),
+ // can be a string or an array of strings.
// options.formData: an array of objects with name and value properties,
// equivalent to the return data of .serializeArray(), e.g.:
// [{name: 'a', value: 1}, {name: 'b', value: 2}]
@@ -50,7 +51,9 @@
'<iframe src="javascript:false;" name="iframe-transport-' +
(counter += 1) + '"></iframe>'
).bind('load', function () {
- var fileInputClones;
+ var fileInputClones,
+ paramNames = $.isArray(options.paramName) ?
+ options.paramName : [options.paramName];
iframe
.unbind('load')
.bind('load', function () {
@@ -101,8 +104,11 @@
return fileInputClones[index];
});
if (options.paramName) {
- options.fileInput.each(function () {
- $(this).prop('name', options.paramName);
+ options.fileInput.each(function (index) {
+ $(this).prop(
+ 'name',
+ paramNames[index] || options.paramName
+ );
});
}
// Appending the file input fields to the hidden form
View
@@ -1,5 +1,5 @@
/*
- * jQuery File Upload Plugin Test 6.5
+ * jQuery File Upload Plugin Test 6.6
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
@@ -552,7 +552,7 @@ $(function () {
paramName: null,
send: function (e, data) {
strictEqual(
- data.paramName,
+ data.paramName[0],
data.fileInput.prop('name'),
'Takes paramName from file input field if not set'
);

0 comments on commit 3de48c6

Please sign in to comment.