所以我有一个从busboy传入的FileStream,我想保存到MongoDB。我想我需要将其作为一个File或某种缓冲区来保存它。我敢肯定,可以通过使用以下方法将其保存到磁盘fs然后读取来完成,但这似乎很麻烦。到目前为止,这是我的完整路由代码:
// Upload a new study plan
router.route("/add").post((req, res, next) => {
let busboy = new Busboy({headers: req.headers});
// A field was recieved
busboy.on('field', function (fieldname, val, valTruncated, keyTruncated) {
if (req.body.hasOwnProperty(fieldname)) { // Handle arrays
if (Array.isArray(req.body[fieldname])) {
req.body[fieldname].push(val);
} else {
req.body[fieldname] = [req.body[fieldname], val];
}
} else { // Else, add field and value to body
req.body[fieldname] = val;
}
});
// A file was recieved
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
const saveTo = path.join('.', filename);
let readFile = null;
file.on("data", () => {
console.log("Got file data!");
})
file.on("end", () => {
//How do I save the file to MongoDB?
})
});
// We're done here boys!
busboy.on('finish', function () {
//console.log(req.body);
console.log('Upload complete');
res.end("That's all folks!");
});
return req.pipe(busboy);
});
我想附加{"pdf": file}到我req.body的剩余数据中...
有只小跳蛙
翻阅古今
相关分类