1
votes

I am trying to upload files using express and formidable (eventualy forwarding to MongoDB and GridFS). I am starting by creating a form with a field of type file. On the action of that field I use the following route....

exports.addItem = function(req, res, next){
  var form = new formidable.IncomingForm(),
    files = [],
    fields = [];
  form
    .on('file', function(field, file) {
      console.log(field, file);
     })
    .on('end', function() {
      console.log('-> upload done');
    });
}

Everything runs fine but when I post I don't see anything in the console and it hangs.

The route looks like the following...

app.post('/item/add', routes.addItem, routes.getPlaylist, routes.index)

Any ideas?

UPDATE

Here is an example of grabbing the file, however, this still doesn't include formidable...

https://gist.github.com/2963261

1

1 Answers

1
votes

The reason it is hanging is because you need to call next() to tell Express to continue.

Also use the bodyParser() middleware in express (included by default) to get the files. Something like this:

exports.addItem = function(req, res, next){
  if(req.files.length > 0)
  {
     // process upload
     console.log(req.files);
  }
  next();
}