Mantas P
Mantas P

Reputation: 21

How to check if file is being uploaded with multer

I am uploading a file using multer but the problem is as I am trying to check if it's being uploaded or not using if (req.body.file) the app will not crash but the browser will say that the page is not available. Is there another way of checking if the file will be uploaded?

Upvotes: 1

Views: 2406

Answers (1)

Medapati Vijay Reddy
Medapati Vijay Reddy

Reputation: 51

    var multer = require('multer');

    var storage = multer.diskStorage({
    	//multers disk storage settings
    	destination: function (req, file, cb) {
    		cb(null, 'public/uploads/')
    	},
    	filename: function (req, file, cb) {
    		//var datetimestamp = Date.now();
    		cb(null, file.originalname)
    	}
    });
    var upload = multer({
    	storage: storage
    })

    router.post('/adduser', upload.single('image'), function (req, res) {
    	console.log(req.body.name);
    	var data = {
    		name: req.body.name,
    		password: req.body.password,
    		image: 'uploads/' + req.file.originalname
    	}
    	users.insert(data, function (err, data) {
    		console.log(data);
    		res.redirect('/home');
    	});
    });
    <form action="/adduser" method="post" enctype="multipart/form-data">
       <input type="text" name="name" />
       <input type="password" name="password" />
       <input type="file" name="image" />
       <input type="submit">
    </form>

Upvotes: 1

Related Questions