I'm having a hard time displaying an image sent by my Node/Express server to my React app. I tried perusing the solutions in other SO's questions, but I could'nt get it to work. Here are my steps
- My Node/Express server sends the image
router.get('/:file_id', (req, res) => {
//... Query file information //
const filePath = `${config.FILE_STORAGE}/${doc._id}`;
res.sendFile(filePath);
});
axiosInstance.get(`/files/${document.file}`)
.then((response) => {
const image_base64 = Buffer.from(response.data).toString('base64');
setThumbnail(image_base64);
})
- And inserted it in an
imgtag
<img src={`data:image/jpg;base64,${thumbnail}`}
But the image doesn't display. I did some additional verifications :
- I manually inserted in
<img>an image that I base64 encoded using an online converter, and it displayed. - I copied the base64 image encoded in step (3) in an online decoder, and it couldn't decode it. So the problem is clearly in either reading the response data or encoding it in base64. Can you help? thanks!
