我正在寻找通过api从Expressjs服务器发送一个GET请求中的多个图像。
我想在MEAN堆栈中创建每个用户上传图像的图库。使用multer上传图像时,图像信息将保存到mongodb,包括上传者的用户ID。
在使用angularjs时,我希望用户可以访问他们之前上传的任何图像。目前我正在基于用户ID在GET请求上发送一个文件。无论如何在一个json中发送多个文件。我目前正在使用Expressjs的res.sendFile
,但还没有找到任何有关发送多个信息的信息。
https://expressjs.com/en/api.html#res.sendFile
这是我当前的获取请求:
exports.getUpload = function(req, res) {
Upload.find({createdby: req.params.Id}).exec(function(err, upload) {
errorhandle.errorconsole(err, 'file found');
console.log(upload[0]);
var options = {
root: '/usr/src/app/server/public/uploads/images'
};
var name = "" + upload[0].storedname +"";
console.log(name);
res.sendFile(name, options,function(err) {
errorhandle.errorconsole(err, 'file sent');
});
});
};
答案 0 :(得分:1)
您不能使用res.sendFile
。事实上,我根本不认为你可以。也许是HTTP/2 Server Push
,但我不确定。
您可以做的是发送一个JSON响应,其中包含指向所有图像的链接:
exports.getUpload = async (req, res) => {
const uploads = await Upload.find({ createdby: req.params.Id }).exec()
const response = uploads.map(image => {name: `https://example.com/uploads/images/${image.storedname}`})
res.json(response)
}
注意错误处理已被忽略。