Skip to content Skip to sidebar Skip to footer

Uploading File To Folder In Node-express Site Using Multer With Mongodb

I have a node-express app that authenticates user data and upload files using multer, when i click on submit, the name, email, username and password fields are stored in the mongod

Solution 1:

create multerhelper.js file in your app nd add below code in it

const multer = require('multer');

// const fs = require('fs');let fs = require('fs-extra');

let storage = multer.diskStorage({
  destination: function (req, file, cb) {
    letId = req.body.id;
    let path = `tmp/daily_gasoline_report/${Id}`;
    fs.mkdirsSync(path);
    cb(null, path);
},
filename: function (req, file, cb) {
    // console.log(file);let extArray = file.mimetype.split("/");
    let extension = extArray[extArray.length - 1];
    cb(null, file.fieldname + '-' + Date.now() + "." + extension);
}
})

 var upload = multer({ storage: storage });

let createUserImage = upload.single('images');


let multerHelper = {
createUserImage,

}

module.exports = multerHelper;

in your user.js file import this file

const multerhelper = require("../multerhelper.js");

router.post('/register',multerhelper  , function(req,res,next){

Solution 2:

Here you are just loading the multer module not using anywhere.

Check the link for full uses :

https://github.com/expressjs/multer

Post a Comment for "Uploading File To Folder In Node-express Site Using Multer With Mongodb"