Skip to content

Instantly share code, notes, and snippets.

@Aakash1103Jha
Last active January 14, 2022 14:26
Show Gist options
  • Save Aakash1103Jha/10d246a7a85a97ddd0fa372f920d7d94 to your computer and use it in GitHub Desktop.
Save Aakash1103Jha/10d246a7a85a97ddd0fa372f920d7d94 to your computer and use it in GitHub Desktop.
Render image using image binary data
// GET DATA FROM DB
// SET DATA IN STATE
// MAP DATA ARRAY AND CONVERT IMAGE BINARY DATA TO BASE64
// USE IT TO CREATE A DATA URL IN IMAGE TAG
import React, { useEffect, useState } from "react"
const App = () => {
const [data, setImageData] = useState([])
const url = "http://localhost:4000/all"
const getData = async (url) => {
try {
const res = await fetch(url)
const data = await res.json()
return data
} catch (e) {
console.error(e)
}
}
useEffect(() => {
getData(url).then((data) => setImageData(data))
}, [url])
return (
<section className={styles.App}>
{data &&
data.map((item) => {
return (
<img key={item._id} src={`data:${item.mimeType};base64,${Buffer.from(item.data).toString("base64")}`} alt="asset" />
)
}
)}
</section>
)
}
export default App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment