Multiple File
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,
});
}
});
Top comments (0)