File uploading with Express 4.0: req.files undefined
Full error message
I'm attempting to get a simple file upload mechanism working with Express 4.0 but I keep getting undefined for req.files in the app.post body. Here is the relevant code:
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
//...
app.use(bodyParser({ uploadDir: path.join(__dirname, 'files'), keepExtensions: true }));
app.use(methodOverride());
//...
app.post('/fileupload', function (req, res) {
console.log(req.files);
res.send('ok');
});
.. and the accompanying Pug code:
form(name="uploader", action="/fileupload", method="post", enctype="multipart/form-data")
input(type="file", name="file", id="file")
input(type="submit", value="Upload")Solutionsource: stackoverflow \u2197
The body-parser module only handles JSON and urlencoded form submissions, not multipart (which would be the case if you're uploading files). For multipart, you'd need to use something like connect-busboy or multer or connect-multiparty (multiparty/formidable is what was originally used in the express bodyParser middleware). Also FWIW, I'm working on an even higher level layer on top of busboy called reformed. It comes with an Express middleware and can also be used separately.
API access
Get this solution programmatically \u2014 free, no authentication.
curl https://depscope.dev/api/error/d90e06c0ae9c407212a5157ec8c3bf345deea9ff8ea442c9f8b537e93af3887ahash \u00b7 d90e06c0ae9c407212a5157ec8c3bf345deea9ff8ea442c9f8b537e93af3887a