1

I'm trying to display a BLOB from from a mySQL table as an image in an EJS file but I cannot figure out how to do it. latestlistings[0].photo is the image I need to display. This is how my EJS file looks like where I need to display the image

<div>
  <%if (latestLocations[0]) {%>
    <%=latestLocations[0].address%>,
    <%=latestLocations[0].city%>,
    <%=latestLocations[0].province_state%>,
    <%=latestLocations[0].country%>
  <%}%>
  <%if (latestListings[0]) {%>
    <%=latestListings[0].price%>
    <img src="data:image/jpeg;base64,<%= latestListings[0].photo %>"/>
  <%}%>
</div>

I'm not modifying the variable at all when I pass it into the EJS template. I'm pulling it straight from the database where it's stored as a LONGBLOB. When I load the page, I just get an image icon which looks like this enter image description here. When I print out the variable right before passing it into the EJS template, it looks like this photo: <Buffer 49 4d 47 5f 32 34 35 35 2e 6a 70 67>. I've been trying for ages trying to get this to work so any help is appreciated!

Mahdi Zarei
  • 5,644
  • 7
  • 24
  • 54
Spectre
  • 138
  • 7
  • `` is not a base64 encoded string. Encode your buffer as base64 string and it should work. – Marc Dec 11 '21 at 17:41
  • I am passing the image from the HTML form into the database through an INSERT statement in mySQL and it is stored like that. Do you know how I could encode it in base64? – Spectre Dec 12 '21 at 21:28
  • https://stackoverflow.com/a/6182519/5781499 – Marc Dec 12 '21 at 23:11

0 Answers0