我正在使用MEAN堆栈,我正在尝试使用Mongo GridFs来存储文件。
这是我到目前为止尝试的代码。
exports.getFile = function(req, res) {
var fileId = req.params.fileId;
gfs.findOne({ _id: fileId }, function (err, file) {
if (err) { res.status(500).send(err) }
console.log('file meta', file);
res.header("Content-Type", file.contentType);
var readstream = gfs.createReadStream({
filename: file.filename
});
//error handling, e.g. file does not exist
readstream.on('error', function (err) {
console.log('An error occurred!', err);
throw err;
});
readstream.on('close', function(file) {
console.log('file content', file);
});
readstream.pipe(res);
});
};
这是我在控制台上获得的:
file meta { _id: 566c0b6ddfb625ae52ce99c7,
filename: 'profile_picture_by_kyo_tux-d4hrimy.png',
contentType: 'binary/octet-stream',
length: 0,
chunkSize: 261120,
uploadDate: Sat Dec 12 2015 17:26:29 GMT+0530 (IST),
aliases: null,
metadata: null,
md5: 'd41d8cd98f00b204e9800998ecf8427e' }
file content undefined
GET /api/users/file/566c0b6ddfb625ae52ce99c7 200 9ms
我没有在网页上获取该文件。 我做错了什么?