Tirlochan Arora
Tirlochan Arora

Reputation: 61

How to send buffer image data from node js to ejs template

I am practicing of uploading and fetching images from database using node js. The uploading part went very well, but fetching part is giving some problems, the code for fetching images is app.get('/images', async (req, res) => { const img = await image(image is the model with type of buffer).find({}); res.render('image', { img }) }. The ejs template part is <% img.forEach(i => { <img src="data:img/png;base64,<%= i.avatar(avatar is property on the image model) %>"> }. Can someone tell how to do this??

Upvotes: 1

Views: 1245

Answers (1)

eol
eol

Reputation: 24565

You should probably map the avatar buffer to a base64-encoded string and pass that to the ejs-template:

app.get('/images', async (req, res) => {
    const img = await image().find({});
    img.avatar = Buffer.from(img.avatar).toString('base64');
    res.render('image', {img});
});

Upvotes: 1

Related Questions