【问题标题】:send image from server to client with REST API使用 REST API 将图像从服务器发送到客户端
【发布时间】:2019-08-07 03:41:29
【问题描述】:

我正在尝试将存储在服务器中(当前在本地存储中)的图像发送到客户端。这是我的部分代码

exports.get_icon = (req, res) => {

  App.findOne({name: req.body.name}, (error, application) => {
    if(error){
      console.log(error);
    } else{
      console.log(application);

      res.status(200).send(application.iconImage) //!!need to do something here
    }
  })
}

这个函数应该获取图像的存储路径,然后将其发送给客户端。当前,服务器发送图像的路径,而不是图像本身。像这样uploads/Twitter/icon.png。那么,知道application.iconImage 给出了图像的路径,我怎么能将图像从服务器发送到客户端呢?

【问题讨论】:

    标签: javascript rest api express mongoose


    【解决方案1】:

    express 中有一个功能是通过文件路径发送文件。

    res.sendFile

    app.get('/getImage/:id', (req, res) => {
        res.sendFile(filepath);
    });
    

    但是,我建议您发送文件路径而不是文件,因为这是最佳做法。

    app.get('/getImage/:id', (req, res) => {
       res.send({ img: filePath });
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      相关资源
      最近更新 更多