multer icon indicating copy to clipboard operation
multer copied to clipboard

Multer not parsing the form data. Always shows undefined

Open kirtanshetty opened this issue 9 years ago • 10 comments

I am getting all three fields body, file and files as undefined no matter what I do .. Please can anyone tell me what am I doing wrong.

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

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

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

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

This is another way in which I tried but it is still undefined.

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

var storage = multer.diskStorage({
  destination: function(req, file, cb){
    console.log("destination");
    var dest = 'images/';
    cb(null, dest);
  }
});

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

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

kirtanshetty avatar Mar 27 '17 10:03 kirtanshetty

How are you posting to the server? Are you using enctype="multipart/form-data" on your form?

LinusU avatar Apr 02 '17 11:04 LinusU

@LinusU ... hey, it got resolved when i remove the content type ... thanks 👍

kirtanshetty avatar Apr 03 '17 06:04 kirtanshetty

No problem 👍

LinusU avatar Apr 03 '17 10:04 LinusU

@LinusU @kirtanshetty Why I have set the contentType to "multipart/form-data", but I still got file undefined in my server code;

I use the FormData Object posting my data like this:

var formData = new FormData(); formData.append('remark','this is the text file') formData.append('license',this.state.file);

jyjin avatar Oct 13 '17 06:10 jyjin

formData.append('key', 'value') adds a text field, not a file. It will be available on req.body (e.g. req.body.key === 'value')

LinusU avatar Oct 13 '17 14:10 LinusU

@LinusU But I need to upload a file by FormData, that file is a file data of one file-input .So how can I upload a file ? See https://github.com/expressjs/multer/issues/532

jyjin avatar Oct 13 '17 14:10 jyjin

@LinusU formData.append can add files / blobs to the formData. It's working for me. https://developer.mozilla.org/en-US/docs/Web/API/FormData/append

leviwheatcroft avatar Jul 01 '18 22:07 leviwheatcroft

https://github.com/musmanalibaloch/express-form-data, Use this example, it uses firebase cloud function and firebase storage to upload file but you can use it for anything , it uses express-multipart-file-parser and works like charm the above link just works perfectly,I tested this code and pushed it to repo for you guys.Just add your configuration and there you go.Cheers.

musmanalibaloch avatar Sep 17 '18 07:09 musmanalibaloch

Make sure you are targeting the right value in the HTML/react code. If you are using React, the onChange property should set the state variable to setVariableFunction(e.target.files[0])

a-box31 avatar Aug 19 '24 21:08 a-box31

邮件我已收到,谢谢!

jyjin avatar Aug 19 '24 21:08 jyjin