简体   繁体   English

使用 node.js 在哈巴狗(玉)上显示来自编码 base64 的图像

[英]Display image from encoded base64 on pug (jade) using node.js

How to decode this data (I'm not sure if what type is this) and display the image.如何解码这些数据(我不确定这是什么类型)并显示图像。 I'm using pug as template engine.我使用哈巴狗作为模板引擎。

Here is the data that I need to display as an image:这是我需要显示为图像的数据:

/9j/4AAQSkZJRgABAQEAYABgAAD/4QBaRXhpZgAATU0AKgAAAAgABQ...and so on /9j/4AAQSkZJRgABAQEAYABgAAD/4QBaRXhpZgAATU0AKgAAAAgABQ...等等

I tried this code in my node.js but it doesn't work我在我的 node.js 中尝试了此代码,但它不起作用

var img = Buffer.from(image).toString('base64');

The variable image is from this response:变量图像来自此响应: 回复

While this code is for passing data to.pug file虽然此代码用于将数据传递到 .pug 文件

res.render('getsurveyemployeeimg', {
   surveyId: results[0].surveyId,
   empNo: results[0].empNo,
   empName: employee.result.fullnameEn,
   empImg: img,
   date: created,
   answers: results
});

.pug file .pug 文件

img(src="data:image/jpg;base64,#{empImg}")

I always keep on getting broken image icon as an output我总是将图像图标损坏为 output

Code is for passing data to.pug file:代码用于将数据传递到 .pug 文件:

res.render('getsurveyemployeeimg', {
   surveyId: results[0].surveyId,
   empNo: results[0].empNo,
   empName: employee.result.fullnameEn,
   empImg: image,
   date: created,
   answers: results
});

.pug file should look like this: .pug 文件应如下所示:

img(src="data:image/jpg;base64," + empImg)

After that, everything should work if empImg actually contains a base64 image.之后,如果empImg实际包含base64图像,则一切都应该正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM