【发布时间】:2020-08-15 14:46:57
【问题描述】:
我正在使用 express、mongodb、multer、ejs 和croppiejs 上传患者照片。当用户上传照片时,他们可以选择裁剪它。我将裁剪的照片保存为名为 croppedPhoto 的字段中的 BLOB 对象。
现在,我想在前端显示裁剪后的照片。我正在传递患者对象(其中包含记录的所有数据字段,包括裁剪照片)。
我正在考虑将该 blob 对象转换为 base64 并显示它。但问题是我不确定如何在 ejs 模板中使用 croppedPhoto 字段值来转换它。
server.js [查找所有患者并传入 ejs 模板 - 还包括croppedPhoto 字段]
app.get('/', async (req, res) => {
const patients = await Patient.find();
res.render('index', { patients: patients });
});
index.ejs [想在img标签中显示照片]
<div class="flex flex-wrap mt-10">
<% patients.forEach(patient => { %>
<div
class="flex flex-col items-center justify-center h-auto lg:h-auto lg:w-32 flex-none bg-cover rounded-t lg:rounded-t-none lg:rounded-l text-center overflow-hidden">
<img src="<%= patient.croppedPhoto %>" class="my-3 w-20 h-20 rounded-full" alt="Patient Photo">
</div>
<% }) %>
</div>
谢谢!!
【问题讨论】:
标签: node.js express blob ejs multer