score:0

//Multer DiskStorage Config
const diskStorage = multer.diskStorage({
    destination: 'assets/pic_upload',
    filename: (req, file, call_back) => {

        //Prepend date to the filename or anything that makes the 
        //File unique so it won't get overwritten
        call_back(null, Date.now() + '_' + file.originalname);
    }

});

//Create Multer Instance
const upload = multer({ storage: diskStorage });

//Picture upload
router.post('/upload--pic', upload.single('file'), (req, res) => {


    //Example DB code from my project. Feel free to 
    //apply your own logic here
    //Update the pic in the DB
    User_DB.findOneAndUpdate({ _id: 'idHere' }, {

        //Store the pic path in the DB
        profImage: (req.file.path).replace('assets/', '')

    })

});

At the front end you will need something like this:

 <form action="/pic_upload" enctype="multipart/form-data"> </form>


Related Query

More Query from same tag