node.js - 如何将缓冲区图像数据从节点 js 发送到 ejs 模板
问题描述
我正在练习使用节点 js 从数据库上传和获取图像。上传部分进行得很顺利,但是获取部分出现了一些问题,获取图片的代码是app.get('/images', async (req, res) => { const img = await image(image is the model with type of buffer).find({}); res.render('image', { img }) }
. ejs 模板部分是<% img.forEach(i => { <img src="data:img/png;base64,<%= i.avatar(avatar is property on the image model) %>"> }
. 有人可以告诉如何做到这一点?
解决方案
您可能应该将avatar
缓冲区映射base64
到 -encoded 字符串并将其传递给 ejs 模板:
app.get('/images', async (req, res) => {
const img = await image().find({});
img.avatar = Buffer.from(img.avatar).toString('base64');
res.render('image', {img});
});
推荐阅读
- swift - 快速链接到 OAuth 错误。从链接中获取授权代码时,我收到以下日志
- python - 在 cx_freeze 中创建 dist 时出错
- sql-server - 无法在 Mac 上的 DBeaver 中下载驱动程序 - 原因:central.maven.org
- kdb - KDB:如何从表中删除其中一列具有空值的行
- react-bootstrap - 安装 React Bootstrap - 无法解决
- mysql - sequelize - 如何计算其他表中的总记录
- c# - 了解 npgsql 命令超时?
- javascript - 如何使用 jquery 在第一个 li 之后附加多个 li?
- ios - 使用一键式 React Native 实现捏缩放
- python - 使用python从完整物理地址中查找纬度和经度