Allow fields that have multiple values

This commit is contained in:
Raymond Feng 2013-07-01 17:04:05 -07:00
parent 82279ccacb
commit cda8ff2e17
1 changed files with 18 additions and 3 deletions

View File

@ -26,6 +26,7 @@ Uploader.prototype.processUpload = function (req, res, cb) {
var client = this.client;
var form = new IncomingForm(this.options);
var container = req.params.container;
var fields = {}, files = {};
form.handlePart = function (part) {
var self = this;
@ -43,7 +44,14 @@ Uploader.prototype.processUpload = function (req, res, cb) {
});
part.on('end', function () {
self.emit('field', part.name, value);
var values = fields[part.name];
if(values === undefined) {
values = [value];
fields[part.name] = values;
} else {
values.push(value);
}
self.emit('field', part.name + '[' + (values.length -1) + ']', value);
});
return;
}
@ -70,13 +78,20 @@ Uploader.prototype.processUpload = function (req, res, cb) {
part.on('end', function () {
writer.end(function () {
self._flushing--;
self.emit('file', part.name + ':' + file.name, file);
var values = files[part.name];
if(values === undefined) {
values = [file];
files[part.name] = values;
} else {
values.push(file);
}
self.emit('file', part.name + '[' + (values.length -1) + ']', file);
self._maybeEnd();
});
});
};
form.parse(req, function (err, fields, files) {
form.parse(req, function (err, _fields, _files) {
cb && cb(err, {files: files, fields: fields});
});
}