Skip to content

add multiple upload by name #282

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

Closed
wants to merge 5 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.upload
*.un~
/node_modules/*
/.idea/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ 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.multipleByName = false;
```
If this option is enabled, the call `form.parse`, argument files will contain arrays of `files` for cases when the name field will contain []. For example image [], or image [item] [left view]

```javascript
form.bytesReceived
```
Expand Down
64 changes: 51 additions & 13 deletions lib/incoming_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function IncomingForm(opts) {
this.type = null;
this.hash = opts.hash || false;
this.multiples = opts.multiples || false;
this.multipleByName= opts.multipleByName || false;

this.bytesReceived = null;
this.bytesExpected = null;
Expand Down Expand Up @@ -81,21 +82,10 @@ IncomingForm.prototype.parse = function(req, cb) {
var fields = {}, files = {};
this
.on('field', function(name, value) {
fields[name] = value;
this.addData(fields, name, value);
})
.on('file', function(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;
}
this.addData(files, name, file);
})
.on('error', function(err) {
cb(err, fields, files);
Expand Down Expand Up @@ -135,6 +125,54 @@ IncomingForm.prototype.parse = function(req, cb) {
return this;
};

IncomingForm.prototype.addData=function(object, name, data){
var name_field,
regexp_name=/[^\[\]]+/,
regexp_name_object=/\[([^\[\]]*)\]/ig,
name_object,
array_name_object=[],
object_fields;

if(this.multipleByName && /\[.*\]/.test(name)){
name_field=regexp_name.exec(name)[0];
if(object[name_field]==undefined || !Array.isArray(object[name_field])){
object[name_field]=[];
}
object_fields=object[name_field];
array_name_object=[];
while(name_object=regexp_name_object.exec(name)){
name_object=name_object[1];
if(name_object==''){
name_object=object_fields.length;
}
array_name_object.push(name_object);
}
for(var i in array_name_object){
if(i<(array_name_object.length-1)){
if(object_fields[array_name_object[i]]==undefined){
object_fields[array_name_object[i]]=[];
}
object_fields=object_fields[array_name_object[i]];
}else{
object_fields[array_name_object[i]]=data;
}
}
}else{
if (this.multiples) {
if (object[name]) {
if (!Array.isArray(object[name])) {
object[name] = [object[name]];
}
object[name].push(data);
} else {
object[name] = data;
}
} else {
object[name] = data;
}
}
};

IncomingForm.prototype.writeHeaders = function(headers) {
this.headers = headers;
this._parseContentLength();
Expand Down
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "formidable",
"description": "A node.js module for parsing form data, especially file uploads.",
"homepage": "https://github.com/felixge/node-formidable",
"version": "1.0.14",
"homepage": "https://github.com/makcbrain/node-formidable",
"version": "1.0.15",
"devDependencies": {
"gently": "0.8.0",
"findit": "0.1.1",
Expand All @@ -24,10 +24,7 @@
},
"repository": {
"type": "git",
"url": "git://github.com/felixge/node-formidable.git"
},
"bugs": {
"url": "http://github.com/felixge/node-formidable/issues"
"url": "https://github.com/makcbrain/node-formidable"
},
"optionalDependencies": {}
}