question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

req.file is always undefined

See original GitHub issue

This 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:closed
  • Created 8 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
otmeekcommented, Mar 7, 2016

Nevermind, I figured it out. enctype is supposed to be multipart/form-data and not multipart/formdata

0reactions
LinusUcommented, Mar 8, 2016

Whoops, sorry about that. Glad you got it resolved 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found