req.file undefined for single
See original GitHub issueHi
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:
- Created 5 years ago
- Reactions:6
- Comments:19 (2 by maintainers)
The multer documentation explicitly states to include enctype=“multipart/form-data” in the form declaration. That did the trick for me.
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.