1

I want to upload a file with a form like this (JADE) :

form(action="/file-upload", name="upload", method="post", enctype="multipart/form-data")
    input(type="file", name="theFile")
    input(type="submit", name="Upload")

this is my app.js :

app.use(express.methodOverride());
app.use(express.multipart());
app.use(express.bodyParser());

and this is how I handle the requests:

app.post('/file-upload',function(req,res){
   console.log('FIRST TEST: ' + JSON.stringify(req.files));
   console.log('second TEST: ' +req.files.theFile.name);
});

and this is my Node.js Console :

FIRST TEST: undefined
TypeError: Cannot Read Property 'theFile' of undefined....etc....

oh and by the way there is a warning from Connect module :

connect.limit() will be removed in connect 3.0
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives

What am I do wrong here ? i have follow advise from here, here, here with nothing works. Thanks for help.....

UPDATE: i just added LOGGER to my apps with result, so it's seems that i got the request:

127.0.0.1 - - [Wed, 19 Feb 2014 11:02:11 GMT] "POST /file-upload HTTP/1.1" 500 - 
"http://localhost:3000/admin/news-add" "Mozilla/5.0 (Windows NT 6.1; WOW64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36"
Community
  • 1
  • 1
DennyHiu
  • 4,861
  • 8
  • 48
  • 80
  • 1
    Express has an example for file upload [here](https://github.com/visionmedia/express/blob/master/examples/multipart/index.js) and according to it it looks like your doing things right on the server backend. Try adding `app.use(express.logger('dev')` to enable some log output to see what you are actually receiving. – Hans Kristian Feb 19 '14 at 10:17
  • just add logger as you suggested. it seems fine, server got the POST request and still 'undefined' – DennyHiu Feb 19 '14 at 11:06
  • See http://stackoverflow.com/questions/19959708/req-files-not-working-node-js-express – Oleg Sklyar Feb 19 '14 at 11:52

1 Answers1

3

I see now... i have

app.use(express.multipart());
app.use(express.bodyParser());
app.use(express.methodOverride());

after app.use(app.router); where it should be declared before, like this :

app.use(express.multipart());
app.use(express.bodyParser());
app.use(express.methodOverride())
app.use(app.router);

now, it's work like a charm....

DennyHiu
  • 4,861
  • 8
  • 48
  • 80