I am trying to upload a file containing text, images, and video, so I am using grid fs stream to upload it but 'fs' only reads files from the 'file system', but the file is submitted from an HTML text editor to the post request. Is there any way I can create a read stream from the post request?
app.post('/blogs', urlencodedParser, (req, res) => {
mongodb.MongoClient.connect(uri, function (error, client) {
assert.ifError(error);
const db = client.db(dbName);
var bucket = new mongodb.GridFSBucket(db);
fs.createReadStream(req.body.title, req.body.desc, req.body.virtual_content).
pipe(bucket.openUploadStream(req.body.title, req.body.desc, req.body.virtual_content)).
on('error', function (error) {
assert.ifError(error);
}).
on('finish', function () {
console.log('done!');
process.exit(0);
});
});
mongodb.MongoClient.connect(uri, function (error, client) {
assert.ifError(error);
const db = client.db(dbName);
var bucket = new mongodb.GridFSBucket(db);
// Use bucket...
});
});