0

I wanted to get the image from MySQL using NodeJS, and EJS template. I think the problem is that I failed to transfer buff data into base64. I have tried many soluction out there but didn't work out. My currect code is ...

Index.ejs

<% for ( var i = newsData.length -1; i >= 0; i--){ %>
<img src="<%= newsData[i].image %>">
<h3><%= newsData[i].title  %></h3>    //Which is working
<p><%= newsData[i].content %></p>     //Which is working

I have tried to use

<%- newsData[i].image %>

Which give me the result of ...

Website show data

routes.js

app.get('/news', isLoggedIn, function({user}, res, next) {

var newsData = '';
db.query('SELECT date,title,content,image FROM news', function(err, rows) {
    if (err) {
        console.log(err);
    }
    var newsData = rows;

    // use index.ejs
    res.render('../views/dashboard/html/table/news.ejs', { title: 'Account Information', newsData: newsData, user});

});

});

By the way, the image format that I stored in Mysql is "Blob". Thanks

kind regards.

  • Refer this link https://stackoverflow.com/questions/7650587/using-javascript-to-display-blob You can't directly use blob. – RITESH ARORA Apr 06 '18 at 10:00

1 Answers1

0

Try Below code

<img src="data:image/jpeg;base64,<%- newsData[i].image %>"/>
Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27