0

I can't seem to get multer to work no matter what I do; I've read everything I could find on the matter, and as far as examples go--I've followed them to the best of my ability.

Here is a JSFiddle link to my work.

I can never seem to get the file to upload, despite it actually uploading. In my code, I have

app.post('/submit', function(req, res) {
  console.log("____ Clicked submit ____");
  console.log(req.body);

  console.dir(req.files);
});

The req.body.attachment will show the filename I attached. However, req.files returns undefined.

I appreciate any and all help.

Nxt3
  • 1,970
  • 4
  • 30
  • 52

1 Answers1

0

You have to use the multer handler somewhere in your middleware chain. You have defined the handler with:

var upload = multer({
  dest: './tmp/'
});

But, you do not use it in your middleware chain.

There are two options: 1) Use a global handler (top of the chain, not recommended) 2) Use it on your submit route (recommended)

Read the docs for specific implementation, but the following should work.

// Multer parses single field "attachment"
app.post('/submit', upload.single('attachment'), function(req, res) {
  console.log("____ Clicked submit ____");
  console.log(req.body);

  // console.dir(req.files);
  // for single field use `req.file`, not `req.files`
  console.log(req.file);
});
undefined
  • 2,154
  • 15
  • 16
  • I had already tried this. I knew I was forgetting something--but this still yields `undefined`. would it possibly have to do with the script I use in my html at the very bottom? – Nxt3 Jun 21 '16 at 00:02
  • Absolutely. You need to treat multipart requests differently over XHR. This may help you: http://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax – undefined Jun 21 '16 at 00:05
  • I was adding that extra script to stop the page from reloading upon submission of the form; how can I stop the page from reloading, while still handling the form stuff on the server? – Nxt3 Jun 21 '16 at 12:22