我正在尝试将服务器(当前在本地存储)中存储的图像发送给客户端。这是我的代码的一部分
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
给出了图像的路径,如何将图像从服务器发送到客户端?
答案 0 :(得分:1)
express中有一个功能,可以通过文件路径发送文件。
app.get('/getImage/:id', (req, res) => {
res.sendFile(filepath);
});
但是,我建议您发送文件路径而不是文件路径,因为这是最佳做法。
app.get('/getImage/:id', (req, res) => {
res.send({ img: filePath });
})