首页 > 解决方案 > 使用 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

标签: node.jsexpressmulter-s3

解决方案


推荐阅读