Reputation: 619
I try to upload a file on my node js server using multer module, but for some reasons the uploaded file is always undefined. I have based my code using the documentation here https://github.com/expressjs/multer with the easy example for one single file.
Router :
var multerMiddleware = multer({"dest":"tmp/"});
var slidController = require('./../controllers/slid.controllers.js');
router.route('/slids')
.post(multerMiddleware.single('file'),slidController.create);
Controller :
SlidController.create = function(request,response) {
var id = Utils.generateUUID();
var fileName = Utils.getNewFileName(id,request.file.originalname);
var type = Utils.getFileType(request.file.mimetype);
var title = request.file.originalname;
//more code not relevant here
I use postman to send a post request with an image associated to the key "file", and still I always get "TypeError: Cannot read property 'originalname' of undefined", because obviously file is undefined.
Upvotes: 1
Views: 5688
Reputation: 99
When you are running a curl request remember to give instead of content-type data.
Upvotes: 0
Reputation: 1493
Try adding body-parser before multer :
var bodyParser = require('body-parser');
var multer = require('multer');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//Multer config here ..
Your problem can also happen because you did not set the Content-Type
header to 'multipart/form-data'
when sending your files
Or because there's no field file
in your form.
Upvotes: 1