0

I used PHPmyAdmin as my localhost, Axios to do backend functions and react to display on the webpage, I created a table that stores an ID and a Blob, since blob can store image data. I used state hooks and render each of the item to be displayed on the page but when I render the page, a broken image has displayed. I tried to console.log() on that image data and it displayed as {type: 'Buffer', data: Array(50639)} on the console browser.

In short, I am trying to retrieve an image from a table in the localhost and display it on the browser

React


function ProjectCard() {

    const [projectCard, setProjectCard] = useState([]);

    const instance = axios.create( {
        baseURL: "http://localhost:3001/api",
    });

    useEffect(() => {
        instance.get("/getAvailableProjects").then((response) => {
            setProjectCard(response.data);
        });
    }, []);

    return (
        <div className="row g-4 py-4 border-bottom">
            {projectCard.map((val) => {
                return (
                    console.log(val.PROJECT_IMAGE) {/* displayed as {type: 'Buffer', data: Array(50639)} */}
                    <div className="col-lg-3 col-md-6 mb-2" key={val.PROJECT_ID}>
                        <img id="project-img" src={val.PROJECT_IMAGE} className="img-fluid" /> {/* BROKEN IMAGE */}
                    </div>
                );
            })}
       </div>
    )
};

export default ProjectCard;

Server



app.get("/api/getAvailableProjects", (req, res) => {

    const sqlQuery = "SELECT *,  from projectdetail";

    db.query(sqlQuery, (_error, result) => {
        res.send(result);
    });
});

Is there a way to convert this blob data into an actual image?

1 Answer 1

1

Checkout this JS object:

<img src={URL.createObjectURL(file) alt="fooBar" />

It should look like this.

URL.createObjectURL: https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried this to create a URL, but then it does not display to the page. I console.log() it again and it gave me a value of "blob:localhost:3000/e1f1a190-955a-4f03-9f46-83a1934d8837"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.