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);
How are you posting to the server? Are you using enctype="multipart/form-data" on your form?
@LinusU ... hey, it got resolved when i remove the content type ... thanks 👍
No problem 👍
@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);
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 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
@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
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.
Most helpful comment
@LinusU ... hey, it got resolved when i remove the content type ... thanks 👍