user3226932
user3226932

Reputation: 2252

How to get uploaded file from clientside in NodeJS?

I'm using the npm multer module in my nodejs file to get formdata from the clientside, which contains the image that I upload from my computer. However, when I try to print out the file on the server side, it is undefined, which I'm guessing that the file wasn't actually sent to the server. When I print it out on the client side, it is there.

main.js (clientside)

var formData = new FormData();
formData.append("image", file);
var r = new XMLHttpRequest();
r.open("POST", "/post");
r.send(formData);

app.js (server side)

var multer = require('multer');

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

app.post('/post', function(req, res) {
    console.log(req.files);

}

Upvotes: 0

Views: 922

Answers (1)

mscdex
mscdex

Reputation: 106736

You have to actually use the middleware. For example:

app.post('/post', upload.single('image'), function(req, res) {
  console.log(req.file);
});

Upvotes: 3

Related Questions