Illep Illep - 4 months ago 77
Node.js Question

Multer gives req.file undefined

I am trying to upload a file using

multer
. Here's the code:

HTML

<form action="/" method="post" encrypt="multipart/form-data" >
<input name="upl" type="file" ></br>
<input type="text" name="onenee" ></br>
<input type="submit" value="submit" />

</form>


NODE JS

var express = require('express');
// var csrf = require('csurf');
var passport = require('passport');
var Product = require('../models/product');
var multer = require('multer');
var upload = multer({ dest: 'public/uploads/' });

var router = express.Router();

router.post('/', upload.single('upl'), function (req, res, next) {
console.log(' Field name1 '+ req.body.onenee);
console.log(' Field name2 '+ req.files);
console.log(' Field name3 '+ req.file);
res.send(req.file);
});


OUTPUT I GET FROM THE CONSOLE


Field name1 hi

Field name2 undefined

Field name3 undefined

POST / 200 15.536 ms - -

Answer

Your form attribute is incorrect, change encrypt to enctype instead. Your file will be available through req.file

Comments