Do stuff with multiple files when uploading them using node-formidable with Express

It works by moving the line below to the bottom of the app.configure function. Read about the importance of order regarding middleware in Express here: http://expressjs.com/guide.html#middleware

app.use(express.bodyParser({ uploadDir:__dirname + '/public/uploads' }));

And by using this handler:

app.post('/upload', function(req, res){
    var form = new formidable.IncomingForm(),
    files = [],
    fields = [];
    form.on('field', function(field, value) {
        fields.push([field, value]);
    })
    form.on('file', function(field, file) {
        console.log(file.name);
        files.push([field, file]);
    })
    form.on('end', function() {
        console.log('done');
        res.redirect('/forms');
    });
    form.parse(req);
});

So req.files could be an array. Try doing console.log(req.files) in the handler and see what it looks like... But I think you're better off just doing it like in their example. You can get it to work by just removing the ../test/common require at the top and changing the constants to static values (eg instead of TEST_TMP use __dirname + '/uploads').

The handler:

var form = new formidable.IncomingForm(),
    files = [],
    fields = [];

form.uploadDir = __dirname + '/uploads';

form
  .on('field', function(field, value) {
    console.log(field, value);
    fields.push([field, value]);
  })
  .on('file', function(field, file) {
    console.log(field, file);
    files.push([field, file]);
  })
  .on('end', function() {
    console.log('-> upload done');
    res.writeHead(200, {'content-type': 'text/plain'});
    res.write('received fields:\n\n '+util.inspect(fields));
    res.write('\n\n');
    res.end('received files:\n\n '+util.inspect(files));
  });
form.parse(req);

The files are now in __dirname + '/uploads' named as per the output on the console (and to the response, as you can see in the end handler).