I am having trouble with the upload destination in nodejs using multer package. The image are uploading in the folder within nodejs. Bt i want to store that at a different Path outside the project root folder. Here is the below code
const imageURL = "http://localhost/demoAdmin/images/";
const imageStorage = multer.diskStorage({
destination: imageURL, // Destination to store image
filename: (req, file, cb) => {
cb(
null,
file.fieldname + "_" + Date.now() + path.extname(file.originalname)
);
},
});
const imageUpload = multer({
storage: imageStorage,
limits: {
fileSize: 1000000, // 1000000 Bytes = 1 MB
},
fileFilter(req, file, cb) {
if (!file.originalname.match(/\.(png|jpg)$/)) {
// upload only png and jpg format
return cb(new Error("Only images allowed"));
}
cb(undefined, true);
},
}).single("image");
imageUpload(req, res, (err) => {
if (err) {
if (err.message) {
return res.status(500).json({
success: 0,
msg: err.message,
});
}
console.log()
return res.status(500).json({
success: 0,
msg: "Only images allowed",
});
}
res.status(200).json({
success: 1,
msg: req.file,
});
});
Any help from your side will be appreciated! Thanks.