【发布时间】:2019-03-27 17:38:34
【问题描述】:
所以我有一个来自 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...
【问题讨论】:
标签: javascript stream busboy