const multer = require('multer'); const path = require('path'); const imageStorage = multer.diskStorage({ // Destination to store image destination: 'assets/images', filename: (req, file, cb) => { // let filename=file.originalname.split(".")[0]; // cb(null, filename + '_' + Date.now() + path.extname(file.originalname)) cb(null, file.originalname) // file.fieldname is name of the field (image) // path.extname get the uploaded file extension } }); const imageUpload = multer({ storage: imageStorage, limits: { fileSize: 1500000 // 1000000 Bytes = 1 MB }, fileFilter(req, file, cb) { if (!file.originalname.match(/\.(png|jpg)$/)) { // upload only png and jpg format return cb(new Error('Please upload a Image')) } // cb(undefined, true) cb(null, true) } }).single("attendancefile"); module.exports = (req, res, next) => { try { // console.log(req); imageUpload(req,res,next, function(err){ try { if (err){ console.log(err); res.status(400).send({ "success":false, "message" : JSON.stringify(err), "data" :[] }); } else { console.log('The filename is ' + res.req.file.filename); // req.append("filename",res.req.file.filename); // req.body.attendancefile=res.req.file.filename; } next(); } catch (error) { console.log("error "+error); } }); } catch (error) { console.log(error); error.message = error.message; error.statusCode = 500; throw error; } }