我有一个用于发布一些文本字段和多个附件的表单。为此,我首先使用gridfs将附件保存到数据库,之后,我将其id插入到集合中。问题是我没有任何想法访问附件的ID。代码如下:
//Saving attachments to gridfs
var part1 = req.files.file1;
var part2 = req.files.file2;
var excel = req.files.fileExcel;
var writeImage1 = gfs.createWriteStream({
filename: 'img_' + part1.name,
mode: 'w',
content_type: part1.mimetype
});
var writeImage2 = gfs.createWriteStream({
filename: 'img_' + part2.name,
mode: 'w',
content_type: part2.mimetype
});
var writeExcel = gfs.createWriteStream({
filename: 'excel_' + excel.name,
mode: 'w',
content_type: excel.mimetype
});
writeImage1.on ('close', function (image1) {
console.log('Enter close writeImage1');
console.log('Id image1 ', image1._id);
});
writeImage2.on('close', function (image2) {
console.log('Enter close writeImage2');
console.log('Id image2 ', image2._id);
});
writeExcel.on('close', function (file) {
console.log('Enter close writeExcel');
console.log('Id Excel ', file._id);
});
writeImage1.write(part1.data);
writeImage1.end();
writeImage2.write(part2.data);
writeImage2.end();
writeExcel.write(excel.data);
writeExcel.end();
//Save to posting collection in mongodb
PostingSecond.create ({
name: body.name,
date: body.date,
place: body.place,
participant: body.participant,
cause: body.cause,
how: body.how,
typePosting: body.typePosting,
//I want to put id images here
imageId: [image1Id, image2Id],
//I want to put id file here
fileId: excelId
}, function (err, posting2) {
if (err) {
console.log(err);
return res.status(400).json('Failed to save document');
} else {
res.json(posting2);
}
});
答案 0 :(得分:0)
如果您使用multer
进行上传,则可以通过以下方式访问文件信息: