'express js upload images to react public folder reloads the page
I am newbie to ReactJs and Express Js. I am trying to upload image to react js from express js API but the problem is when I save the image in React public folder react/public/uploads then the page reloads after image upload successfully but when I upload the image in express public folder /server/public/uploads then it works fine.
this is my code to upload image
const filespath = '../public/uploads/profile/';
router.post('/updateimage', upload.single("file","id"),async (req,res) => {
const multerReq = req;
try {
if (!multerReq.file) {
res.status(500).json({ status: false, msg: "no file provided" });
} else {
const fileName = multerReq.file.filename;
const updatedPost = await User.updateOne(
{ _id: req.body.id},
{ $set:
{
avatar: fileName
}
}
);
res.status(200).json({status: updatedPost, msg:"Profile Photo Updated Successfully"});
}
}catch(err){
res.json({ message: err});
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
