Search code examples
node.jsexpressmultipartform-datamulter

Multer not uploading the image in express app


I have written this simple application to test multer to upload files into my server. I am trying to test this using postman where I have selected form-data option, added a key to my image and uploaded the image using the button next to it. The API is called correctly but the image doesn't get uploaded.

var express = require("express");
var app = express();
var multer = require('multer');

var upload = multer({
  dest: 'images/',
});

app.post('/uploadchq', upload.any(), function(req, res, next) {
  console.dir("file", req.file);
  console.dir("files", req.files);
  console.log("body", req.body);
  next();
  res.sendStatus(200);
});

app.listen(9999);
console.log("listening to port", 9999);

Multer is creating the image directory that I have specified in the dest field, but req.file , req.files , req.body all the field are empty. I tried using upload.single instead of any with the key name as the parameter but it still doesn't work. What is the problem ?

Edit: In reply to Gaurav's comment. Here is the screen shot of postman. I have changed the key name from chqImg to file instead. Screenshot of postman


Solution

  • I figured out what was wrong. I just unchecked the header field in postman which was setting the Content-type to multipart/form-data . However, I am not able to understand why it won't work with it.