首页 > 解决方案 > 使用 muller 存储的 MEAN 堆栈显示图像

问题描述

我正在为我的 Web 应用程序使用 MEAN 堆栈。我成功地能够在节点服务器上存储多个图像文件,并将其路径与其他信息一起保存在 MongoDB 中。当我发出获取数据的请求时。如何将图像文件数组和其他信息发送回客户端?

数据库中存储的内容是

[
  {
    "paintingFileLocations": [
      {
        "fieldname": "artFileLocations",
        "originalname": "logo1.png",
        "encoding": "7bit",
        "mimetype": "image/png",
        "destination": "artImages/paintings",
        "filename": "painting&1619496101680&logo1.png",
        "path": "artImages/paintings/painting&1619496101680&logo1.png",
        "size": 95523
      },
      {
        "fieldname": "artFileLocations",
        "originalname": "logo2.png",
        "encoding": "7bit",
        "mimetype": "image/png",
        "destination": "artImages/paintings",
        "filename": "painting&1619496101684&logo2.png",
        "path": "artImages/paintings/painting&1619496101684&logo2.png",
        "size": 94132
      }
    ],
    "paintingName": "Painting 1",
  }
]

这是我的获取请求

router.get('/', function(req, res) {
    Paintings.find({})
        .then(paintings => { res.send(paintings) })
        .catch(error => console.log(error))
});

标签: node.jsmean-stack

解决方案


所以你正在为此使用NodeJS......你将拥有的东西是

app.post('/route', (req, res, next) => {
  ...
  //code to upload the image
  ...

  // fetch this object from MongoDB
  const object = Model.find('---');

  // here return the object in the response
  res.status(200).send(object)
})

推荐阅读