Thorsten Hans
Thorsten Hans

Reputation: 2683

Uploading files with ExpressJS

I'm using latest NodeJS and ExpressJS to write a small app. Currently I stuck on uploading a file. :>

routes are configured this way

app.get('/Share', share.index);
app.post('/Share/Process', share.processFile);

The index view looks like the following

form.form-horizontal(action='/Share/Process', method='post')
  legend share a file
  div.control-group
    label.control-label(for='item[actual]') File
    div.controls
      input(type='file', name='item[actual]', required='required')

When I follow the ExpressJS API documentation, I should have access to the file, by using req.files, which is undefined within the share.processFile method

exports.processFile = function(req,res){
  console.log(req.files); // undefined
  console.log(req.body.item.actual); // filename as string
  res.render('share/success', {navigation: { shareClass : 'active'}, downloadKey: 'foo'});
};

Upvotes: 0

Views: 404

Answers (1)

Golo Roden
Golo Roden

Reputation: 150614

Try changing the form encoding to multipart/form-data, IIRC this should be set for transferring files.

Upvotes: 2

Related Questions