1

I'm trying to upload a file with connect-multiparty on my api but I can't make it works.

Here my code (a sample, because my api have a lot of working routes):

var express             = require('express');
var bodyParser          = require('body-parser');
var multipart           = require('connect-multiparty');
var app                 = express();
var multipartMiddleware = multipart({ uploadDir: './imagesPath' });

// Define middleware
app.use(bodyParser.urlencoded({ extended: true })); // Support encoded bodies
app.use(bodyParser.json());                         // Support json encoded bodies

var router = express.Router();                      // Get an instance of the express Router

router.post('/testFileUpload', multipartMiddleware, function(req, res) {
    console.log(req.body, req.files);

    // Some other code
});

When I try to upload a file both req.body and req.files are empty {}

I know Body-Parser doesn't support multipart/form-data anymore so I'm trying to find a way to use it with another package but with no success so far.

I've tried with busboy, connect-busboy, multer, formidable, express-formidable, express-fileupload but everytime req.files is undefined, so I kinda feel I've make some progress with connect-multiparty by having req.files empty.

I've seen some topics with similar problems like this one, this one or this one but unfortunatly none of those helped me.

In client side I'm using Advanced REST Client and Postman.

Any ideas what I'm doing wrong ?

Community
  • 1
  • 1
Maugun
  • 11
  • 3

2 Answers2

1

To whoever has this problem, I found removing the Content-Type headers on Postman with value "application/javascript" worked for me. I just hadn't noticed the whole time, while I tried different packages same as OP.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
heligon
  • 11
  • 1
0

put enctype="multipart/form-data" in your tag form on html

Betini O. Heleno
  • 326
  • 4
  • 15