快速发送以base-64编码的png图像 [英] Express send base-64 encoded png-image
本文介绍了快速发送以base-64编码的png图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的node.js应用中,我试图用图像进行响应.
In my node.js app I`m trying to respond with an image.
此图像在Postgresql之前保存为文本.
This image was saved before postgresql as text.
文本看起来像这样:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAAE
但是当我尝试将其作为图像返回时:
res.type('image/png');
res.send(image_string);
或二进制:
res.send(image_string,'binary');
它显示了一个空的图像元素:
It shows a empty image-element:
我怎么了?谢谢
推荐答案
我通过使用缓冲区解决了这个问题:
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);
这篇关于快速发送以base-64编码的png图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文