I am currently trying to make a simple form where user can add their username, password and image to the database (I use mySQL for the database). I store the image as a blob data type and in the react file, I first converted it to Base64 before adding it to the database.
const handleUpload = async (e) => {
const file = e.target.files[0];
const base64 = await convertBase64(file);
setDataUri(base64);
// console.log("Converted to Base64: " + base64)
}
const convertBase64 = (file) => {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = () => {
resolve(fileReader.result);
}
fileReader.onerror = (error) => {
reject(error);
}
})
}
And when I called the data from the database, it is in this form:
{
...,
"image": {
"data": (2074) [100, 97, 116, 97, 58, 105, ...],
"type": "Buffer",
}
}
So I converted the image.data to base64 before displaying it as an image.
const convertImage = (file) => {
var convert = Buffer.from(file.data).toString('base64');
return convert
}
...
<img src={`data:image/png;base64,${convertImage(data.image)}`} style={{height: "250px"}} />
However, I dont see any images. I need guidance for this; perhaps I converted to the wrong format, maybe I miss something along the way or is it a backend problem? Any pointers or hints are welcomed!
Edit: Forgot to state my question.
Found the way to solve it: Solution