req.file is always undefined
See original GitHub issueThis is my first time using multer, so I’m probably missing something, but I’m trying to make it possible to upload a file to the server using post, but req.file is always undefined. Here is my code:
'use strict';
var express = require('express');
var path = require('path');
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var app = express();
require('dotenv').load();
app.use(express.static(path.join(__dirname, 'main')));
app.post('/', upload.single('upl'), function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
console.log(req.file);
res.status(204).end();
})
var port = process.env.PORT || 8080;
app.listen(port, function () {
console.log('Node.js listening on port ' + port + '...');
});
and the form:
<form class="uploadForm" action="/" method="post">
<label class="control-label">Select File</label>
<input name="upl" id="input-1" type="file" class="file">
</form>
Please let me know what I’m doing wrong. Thank you!
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
javascript - multer - req.file always undefined - Stack Overflow
I am trying to make a web app that you can upload files to using express and multer, and I am having a...
Read more >req.file undefined for single #714 - expressjs/multer - GitHub
Hi I'm trying to upload a single file and then send it to another server, I need the buffer but no matter what...
Read more >req.file.path undefined multer - You.com | The AI Search ...
Req.file Is Undefined Multer When working with the Multer library in Node.js, you may encounter the “req.file is undefined” error. This can ...
Read more >Uploading error req.file is undefined and path is also ...
In route: var multer = require('multer'); var storage = multer.diskStorage({ filename: function(req, file, callback) { callback(null, ...
Read more >Express multer middleware
Accepts all files that comes over the wire. An array of files will be stored in req.files . WARNING: Make sure that you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Nevermind, I figured it out. enctype is supposed to be
multipart/form-data
and notmultipart/formdata
Whoops, sorry about that. Glad you got it resolved 👍