Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Off-by-default support for HTML5 multiple attribute in form.parse files argument #272

Merged
merged 2 commits into from

3 participants

@kethinov

Closes #232 and #115.

I've reviewed the previous PRs on this and agreed with @felixge that they needed more work.

This PR is based on @aaronSig's PR #115 but hides the feature behind an off-by-default opt to prevent existing test cases from breaking. I also cleaned up the code a bit.

@Jammerwoch

Looks good. Certainly meets my needs, and is almost identical to the PR I was going to submit.

@felixge felixge merged commit d12299b into felixge:master

1 check passed

Details default The Travis CI build passed
@felixge
Owner

thx. test case would be nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 1 deletion.
  1. +5 −0 Readme.md
  2. +13 −1 lib/incoming_form.js
View
5 Readme.md
@@ -118,6 +118,11 @@ form.hash = false;
If you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`.
```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.
+
+```javascript
form.bytesReceived
```
The amount of bytes received for this form so far.
View
14 lib/incoming_form.js
@@ -30,6 +30,7 @@ function IncomingForm(opts) {
this.headers = null;
this.type = null;
this.hash = opts.hash || false;
+ this.multiples = opts.multiples || false;
this.bytesReceived = null;
this.bytesExpected = null;
@@ -83,7 +84,18 @@ IncomingForm.prototype.parse = function(req, cb) {
fields[name] = value;
})
.on('file', function(name, file) {
- files[name] = file;
+ if (this.multiples) {
+ if (files[name]) {
+ if (!Array.isArray(files[name])) {
+ files[name] = [files[name]];
+ }
+ files[name].push(file);
+ } else {
+ files[name] = file;
+ }
+ } else {
+ files[name] = file;
+ }
})
.on('error', function(err) {
cb(err, fields, files);
Something went wrong with that request. Please try again.