Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 13, 2022 04:11 pm GMT

Uploading multiple and Single files using Node.js and Express.js with MongoDB.

Multiple File

Image description

As shown in the above image of that we have to upload 3 files but that should be of different sections EMD,PAN,AADHAR and if we use the .multiple() the at a time we can select multiple file for only one field, but to do for different fields we shoud use .any(), In this you can type any number of file paths.

const upload_PAN = multer({        storage: multer.diskStorage({            destination: function (req, file, cb) {                cb(null, "uploads_tender")            },            filename: function (req, file, cb) {                let name = file.originalname;                cb(null, file.fieldname + "_" + Date.now() + ".pdf")            }        })    }).any('PAN_file', 'EMD_file', 'Aadhar_file')

Single File

To upload for single file only one thing we have to change
i.e.,

const upload_PAN = multer({        storage: multer.diskStorage({            destination: function (req, file, cb) {                cb(null, "uploads_tender")            },            filename: function (req, file, cb) {                let name = file.originalname;                cb(null, file.fieldname + "_" + Date.now() + ".pdf")            }        })    }).single('Aadhar_file')

Calling Function is the same for both.

  app.post("/upload_tender_file", upload_PAN, (req, res) => {        let k = req.body;        let f = req.files;        let temp = req.files.length;        let edm=f[0];        let pan=f[1];        let aadhar=f[2]        console.log(temp);        console.log(k);        let size = 1;        if (req.session && req.session.userid) {            res.json({                status: "warn",                message: "Session already exists !",                isLogged: true,                lastUpdated: req.session.lastUpdated,                isLatest: false,                profile: req.session.profile,            });        }        // check if any value is not null        else if (size != 0            && edm            && pan            && aadhar            && k.tenderName            && k.email            && k.tenderValue            && k.amountWords            && k.endDate) {            // check if record already exists...            db.collection("tender_files").findOne(                { projection: { _id: 1, email: 1, tenderName: 1 } },                (error, result) => {                    if (result && result._id) {                        res.json({                            status: "error",                            message: "File already exists !",                            isLogged: false,                        });                    }                    // tenserName doesn't exists, create one                    else {                        let obj = {                            tenderName: k.tenderName,                            email: k.email,                            profile: {                                tenderName: k.tenderName,                                email: k.email,                                endDate: k.endDate,                                amountWords: k.amountWords,                                edm: edm,                                pan:pan,                                aadhar:aadhar,                                tenderValue: k.tenderValue,                            },                        };                        db.collection("tender_files").insertOne(obj, (error, results) => {                            if (error) {                                res.json({                                    status: "error",                                    message: error,                                    isLogged: false,                                });                                throw error;                            }                            // Records inserted, auto log in                            else {                                // log it in                                res.json({                                    status: "success",                                    message: "File Uploaded !",                                    isLatest: true,                                    isLogged: true,                                    profile: obj.profile,                                });                            }                        });                    }                }            );        } else {            res.json({                status: "error",                message: "Empty or invalid data",                isLogged: false,            });        }    });

Original Link: https://dev.to/abhishek_159/uploading-multiple-and-single-files-using-nodejs-and-expressjs-with-mongodb-493n

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To