简体   繁体   中英

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?


  
 
  
  
    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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM