From cda8ff2e172fb50db61fde4c9f774404cecc2568 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 1 Jul 2013 17:04:05 -0700 Subject: [PATCH] Allow fields that have multiple values --- lib/storage-handler.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/storage-handler.js b/lib/storage-handler.js index 07b6dbe..9e3181b 100644 --- a/lib/storage-handler.js +++ b/lib/storage-handler.js @@ -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}); }); }