Display images using Base64 and Node.js


I'm trying to get some blob files (images) and then display it on the screen using base64.

This is my node.js code:

var queryimage = "SELECT iproduct FROM images";
connection.query(queryimage, function(err, rows, fields){
    socket.emit('image_prova', new Buffer(rows, 'binary').toString('base64'));

Then I'm getting the suposed string:

websocket.on('image_prova', function(data){
    $('#imagehere').append('<img src=data:image/jpeg;base64,'+data+' />');

The image is not being displayed and the string given is: AA==

I don't understand why...!

Problem courtesy of: Marc Ortiz


You are passing rows instead of rows[0] to Buffer. You only requested one, but it is still an array, so you need to access the one you actually want.

If that doesn't work, let me know.

Solution courtesy of: loganfsmyth


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.