I loaded a 56kb image to a table in a MySQL Db via PHP myAdmin. The value says "[BLOB-56.2 KiB]. I am trying to retrieve this image from my node.js backend and display it on my React.js front end. I can retrieve it as a buffer but I am unsure how to actually get the image back from this. Here is my backend retrieval...
app.post("/api/get-camera-image", (req, res) => {
const device = req.body.device
const sql = "SELECT image FROM `deviceInformation` WHERE device='" + device + "'";
dbUser.query(sql, (err, result) => {
console.log(result)
res.send(result)
res.end()
})
})
and the console.log(result) produces this in the console...
[
RowDataPacket {
image: <Buffer ff d8 ff e1 02 b7 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 09 01 03 00 03 00 00 00 01 00 06 00 00 01 00 00 04 00 00 00 01 00 00 02 a3 01
10 00 02 ... 57470 more bytes>
}
]
My front end looks like this...
useEffect(() => {
Axios.post("http://localhost:3001/api/get-camera-image", {device: props.device}, {responseEncoding: 'binary'})
.then((response) => {
const blob = new Blob( response.data );
const url = URL.createObjectURL( blob );
setFileInDB(url)
})
}, [props.device])
and I am trying to display the fileInDB as an image like so...
return (
<div>
<img height='100' src={fileInDB} alt="Nothing in DB" />
</div>
)
I successfully converted the response.data, which is a buffer, into a blob. I then create a url for the blob and stored it in fileInDB state variable. If I console.log(fileInDB) I get this...
blob:http://localhost:3000/da240ace-8fed-4781-b06b-6ae4136853bd
This would normally work if it were getting the picture from a website but this doesn't work for my case.