Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File upload sends undefined request.files #2602

Closed
ulisesantana opened this issue Dec 30, 2016 · 8 comments
Closed

File upload sends undefined request.files #2602

ulisesantana opened this issue Dec 30, 2016 · 8 comments

Comments

@ulisesantana
Copy link

ulisesantana commented Dec 30, 2016

I was building an image uploading API in Express. When I tried by form-data body and post method request.files always come undefined. If I try with a plain html form, request.files come with all the data and the API works fine.

Postman Version: 4.9.2
App: Chrome App (Running on Chrome 54.0.2840.100)
OS details: Ubuntu 16.04 x64
Is the Interceptor on and enabled in the app: No
Did you encounter this recently, or has this bug always been there: Recently.
Expected behaviour: request.file come with all the form data.

This is the code I use for uploading images:

'use strict';

const express = require('express');
const path = require('path');
const crypto = require('crypto');
const multer = require('multer');

const storage = multer.diskStorage({
  destination: './server/uploads/',
  filename: function (req, file, cb) {
    crypto.pseudoRandomBytes(16, function (err, raw) {
      if (err) return cb(err)

      cb(null, raw.toString('hex') + path.extname(file.originalname))
    })
  }
});

const uploads = multer({ storage: storage })
let api = express.Router();

api.post('/upload-image/:id', uploads.any(), (req, res) => {
  let imageId = req.params.id;
  let fileName = 'Not uploaded';
  console.log('req.files => '+req.files);

  if (req.files) {
    fileName = req.files[0].filename;

    Image.findByIdAndUpdate(imageId, {picture: fileName}, (err, imageUpdated) => {
      if (err) {
        res.status(500).send({
          message: 'Request error'
        });
      } else {
        if (!imageUpdated) {
          res.status(404).send({
            message: 'Image not updated'
          });
        } else {
          res.status(200).send({
            image: imageUpdated
          });
        }
      }
    });

  } else {
    res.status(200).send({
      message: 'Image for uploading not found.'
    });
  }

});

Testing API from Postman
Testing API from Postman

Testing API from plain HTML form
Testing API from plain HTML form

@vegetableman
Copy link

vegetableman commented Jan 17, 2017

@ulisesantana Thanks for letting us know about this issue 👍 . Am guessing you must have figured this out by now, this issue is not related to Postman, seems to be most likely, an express middleware issue, related link:- http://stackoverflow.com/questions/23114374/file-uploading-with-express-4-0-req-files-undefined.

Closing this, feel free to reopen if that's not the case.

@cyrilchapon
Copy link

cyrilchapon commented Mar 17, 2017

This seems to be pretty much related to postman rather than express.

And this is a working workaround.

Need to be reopened

@gabrielstuff
Copy link

Got the same issue. Closing tab and reopening it clear the issue out. Looks more POSTMAN related than Multer. This solution http://stackoverflow.com/questions/35851660/multer-req-file-always-undefined/38461398#38461398

@MaximeMaillet
Copy link

MaximeMaillet commented Jul 25, 2017

Hi. Got the same issue and I found solution.
I unchecked "Content-Type: x-www-form-urlencoded" in Headers.
(postman 5.0.2)

NodeJS side : I use https://www.npmjs.com/package/express-fileupload

@sdnts
Copy link

sdnts commented Jul 26, 2017

I'm reopening this. Can you guys also try this out on or native apps and let me know if you see this there as well?
I'd recommend switching to native apps anyway since Chrome apps will be deprecated by Chrome soon.

@sdnts sdnts reopened this Jul 26, 2017
@cyrilchapon
Copy link

@MaximeMaillet this is perfectly normal. Both Multer and express-fileupload only handle multipart data, which must be set in the Content-Type header.

@madebysid, for myself, since recent updates, I don't have that issue anymore, in chrome extension

Need more testing I think though

@marcoRamonS
Copy link

first i think what node version is the problem but not. i have installed this:
Postman for Chrome
Version 5.1.3
linux / x86-64
Chrome 53.0.2785.89

and i get the same error: TypeError: Cannot read property 'length' of undefined

@sdnts
Copy link

sdnts commented Aug 25, 2017

@marcoRamonS Where do you see this error? In Postman's DevTools, or the response section?
Can you also try out the native Postman apps and see if it persists?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants