Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express send base-64 encoded png-image

In my node.js app I`m trying to respond with an image.

This image was saved before postgresql as text.

The text looks just like this:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAAE

But when I try to return it as an image:

    res.type('image/png');
    res.send(image_string); 

Or binary:

     res.send(image_string,'binary'); 

It shows a empty image-element: enter image description here

What do I wrong?Thanks

like image 983
John Smith Avatar asked Dec 04 '15 23:12

John Smith


1 Answers

I solved it by using a buffer:

const im = image_string.split(",")[1];

const img = Buffer.from(im, 'base64');

res.writeHead(200, {
   'Content-Type': 'image/png',
   'Content-Length': img.length
});

res.end(img); 
like image 57
John Smith Avatar answered Sep 20 '22 06:09

John Smith