Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for array-like fields #340

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ If you want checksums calculated for incoming files, set this to either `'sha1'`
```javascript
form.multiples = false;
```
If this option is enabled, when you call `form.parse`, the `files` argument will contain arrays of files for inputs which submit multiple files using the HTML5 `multiple` attribute.
If this option is enabled, when you call `form.parse`, the `files` argument will contain arrays of files for inputs which submit multiple files using the HTML5 `multiple` attribute. Also, the `fields` argument will contain arrays of values for fields that have names ending with '[]'.

```javascript
form.bytesReceived
Expand Down
16 changes: 15 additions & 1 deletion lib/incoming_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,21 @@ IncomingForm.prototype.parse = function(req, cb) {
var fields = {}, files = {};
this
.on('field', function(name, value) {
fields[name] = value;
if (this.multiples && name.substr(name.length-2) === '[]') {
name = name.substr(0, name.length-2);
if (fields[name]) {
if (!Array.isArray(fields[name])) {
fields[name] = [fields[name]];
}
fields[name].push(value);
}
else {
fields[name] = value;
}
}
else {
fields[name] = value;
}
})
.on('file', function(name, file) {
if (this.multiples) {
Expand Down