Express send base-64 encoded png-image

早过忘川 提交于 2020-06-16 11:38:02

问题


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:

What do I wrong?Thanks


回答1:


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); 


来源:https://stackoverflow.com/questions/34099000/express-send-base-64-encoded-png-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!