Browse files

files

  • Loading branch information...
1 parent 443152d commit 6ffd5ad14234ecec6a9fe107a8fb50f63b26d990 @tj tj committed Nov 17, 2011
Showing with 40 additions and 2 deletions.
  1. +11 −2 lib/middleware/bodyParser.js
  2. +29 −0 test/bodyParser.js
View
13 lib/middleware/bodyParser.js
@@ -125,8 +125,14 @@ exports.parse['multipart/form-data'] = function(req, fn){
query.push(name + '=' + val);
});
- form.on('file', function(){
- console.log(arguments);
+ form.on('file', function(name, file){
+ if (Array.isArray(files[name])) {
+ files[name].push(file);
+ } else if (files[name]) {
+ files[name] = [files[name], file];
+ } else {
+ files[name] = file;
+ }
});
form.on('error', fn);
@@ -135,6 +141,9 @@ exports.parse['multipart/form-data'] = function(req, fn){
query = query.join('&');
query = qs.parse(query);
req.body = query;
+ Object.keys(files).forEach(function(name){
+ req.body[name] = files[name];
+ });
fn();
});
View
29 test/bodyParser.js
@@ -57,6 +57,35 @@ describe('connect.bodyParser()', function(){
});
})
+ it('should support files', function(done){
+ var app = connect();
+
+ app.use(connect.bodyParser());
+
+ app.use(function(req, res){
+ req.body.user.should.eql({ name: 'Tobi' });
+ req.body.text.constructor.name.should.equal('File');
+ res.end(req.body.text.name);
+ });
+
+ app.request()
+ .post('/')
+ .set('Content-Type', 'multipart/form-data; boundary=foo')
+ .write('--foo\r\n')
+ .write('Content-Disposition: form-data; name="user[name]"\r\n')
+ .write('\r\n')
+ .write('Tobi')
+ .write('\r\n--foo\r\n')
+ .write('Content-Disposition: form-data; name="text"; filename="foo.txt"\r\n')
+ .write('\r\n')
+ .write('some text here')
+ .write('\r\n--foo--')
+ .end(function(res){
+ res.body.should.equal('foo.txt');
+ done();
+ });
+ })
+
it('should work with multiple fields', function(done){
app.request()
.post('/')

0 comments on commit 6ffd5ad

Please sign in to comment.