node.js - 使用 amazon multers3 上传后如何使用 Expressjs 在视图中渲染图像
问题描述
请问我如何告诉我的视图使用 ejs 模板在视图中显示图像。
我正在上传到 Amazon multers3,并且图像已成功上传到此目录中public/uploads/postImages
这是我在路由文件中的上传。
const upload = multer({
storage: multerS3({
s3: s3,
bucket: bucketname,
s3BucketEndpoint:true,
endpoint:"http://" + bucketname + ".s3.amazonaws.com",
key: function (req, file, cb) {
cb(null, 'public/uploads/postImages/' + file.originalname);
}
})
})
router.post('/', upload.single('cover'), async (req, res, next) => {
const fileName = req.file != null ? req.file.filename : null
const post = new Post({
title: req.body.title,
description: req.body.description,
from: req.body.from,
postImage: fileName,
})
try {
const newPost = await post.save()
res.redirect('/posts')
} catch {
if (post.postImage != null) {
removePostImage(post.postImage)
}
renderNewPage(res, post, true)
}
});
这是我的帖子/index.ejs
<main role="main">
<ul class="flexgrid columns-news">
<% posts.forEach(post => { %>
<li>
<figure class="gallery-image">
<img src="/public/uploads/postImages/<%= post.postImage %>" >
</figure>
</li>
<% });%>
</ul>
</main>
我如何告诉我的视图寻找里面的图像public/uploads/postImages
?
解决方案
推荐阅读
- r - 有人可以在 gsub() 中解释这个正则表达式吗?
- javascript - 如何在页面刷新时保持 GridView 单元格背景颜色
- jsf - 移动设备上的 Primefaces Timeline NumberFormatException (NaN)
- python - Reportlab 表缺少底线
- semantic-web - 从大量异构 RDF 数据集中提取实体的所有属性和值
- r - 有没有办法计算数据框每一列中 NA 的百分比,但是将 df 分成不同的组?
- spring - 如何为多模块项目配置 Flyway?
- python - 值之间的相关性
- javascript - 使用 Kendo.culture 以欧元货币格式写入百分比值
- arrays - 将每两个元素 bash 放入数组中