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 undefined for single

See original GitHub issue

Hi

I’m trying to upload a single file and then send it to another server, I need the buffer but no matter what I do req.file is always empty, req.files.file is not however, but req.files.file.buffer is not there either, my code is

// The Client:

const uploader = document.createElement(‘input’) uploader.type = ‘file’ uploader.accept = ‘.csv’ uploader.name = ‘my_file’ uploader.addEventListener(‘change’, event => { const formData = new FormData() formData.set(‘enctype’,‘multipart/form-data’) // I also tried formData.enctype = ‘multipart/form-data’ formData.append(‘file’, uploader.files[0])

window.fetch(url, { headers: { ‘Accept’: ‘application/json’ }, body: formData, method: ‘POST’, mode: ‘no-cors’, credentials: ‘same-origin’ }) .then(response => handleResponseErrors(response, url)) })

// The server:

const storage = multer.memoryStorage() console.log({ storage: storage }) const upload = multer({ storage: storage, inMemory: true }).single(‘my_file’) upload(req, res, (err) => { if (err) throw err

// I need a buffer here console.log({ content: req.file )} /// this is undefined console.log({ content: req.files.file )} /// this is defined console.log({ content: req.files.file.buffer )} /// this is undefined })

Like I said, req.file is empty even though I’m using .single(‘my_file’) , I also don’t understand what the difference between req.file and req.files.file (which I do end up having but still without buffer)

I also want to add that uploading the file from the client directly to the end server without passing through node.js and multer works but as a requirement I need to pass through nodejs and I just cannot make it work

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:19 (2 by maintainers)

github_iconTop GitHub Comments

17reactions
mdehooghcommented, Dec 20, 2019

The multer documentation explicitly states to include enctype=“multipart/form-data” in the form declaration. That did the trick for me.

17reactions
tomislav13commented, Sep 2, 2019

In my case, I was using both multer AND express-fileupload packages. Just remove express-fileupload or similar file upload library and it should work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - multer - req.file always undefined - Stack Overflow
This is single file demo code for uploading file using multer in Node and postman. In POSTMAN Select POST method and select form-data...
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 >
Node.js - req.files returns "undefined" when sending POST to ...
I just realized, one of the properties is "body" and it is attached to an empty object, so I guess nothing is being...
Read more >
multer multiple upload, req.files.filename undefined - Laracasts
helpp, i get error, undefined when i console.log(req.files.filename). Copy Code const storage = multer.diskStorage({ destination : path.join(__dirname + '.
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 >

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