我可以将文件上传到我的数据库(Mongodb),但是,可以知道谁使用护照上传了文件吗? 我尝试了类似的方法,但是没有用。
app.post('/upload', upload.single('file'), (req, res) => {
// res.json({ file: req.file });
db.collection("anyCollection").insertOne({
fileUploaded : file,
uploadedBy : req.user
});
});
Gridfs初始化:
let conn = mongoose.createConnection("mongodb://localhost:27017/FYP")
// Init gfs
let gfs;
conn.once('open', () => {
// Init stream
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('requests');
});
我的存储引擎:
// Create storage engine
const storage = new GridFsStorage({
url: "mongodb://localhost:27017/FYP",
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'anyCollection'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
有没有办法知道上传的用户是谁?