Usually I create XLSX binary object by loading data from MongoDb. This is then streamed to the client in a http response. This is the code I use which works perfectly.
Server side code:
//Row data (rows) generated from database
conf.rows = rows;
//Only creates Headers from columns in existing data
const data = lib.objectToSheet(conf);
//Create sheet & workbook
const ws = XLSX.utils.aoa_to_sheet(data, {cellDates: true});
const wb = {SheetNames: ["MBS"], Sheets: {MBS: ws}};
const wopts = {bookType: 'xlsx', bookSST: false, type: 'binary', compression:true};
//Create binary data to write to client
const wbout = XLSX.write(wb, wopts);
const fileName = 'FileName.xlsx';
//write workbook to client
request.response.ContentType = "application/file";
request.response.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8');
request.response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
request.response.end(wbout, 'binary');
In stead of streaming this back to the client I want to save the binary object (wbout) as a file in MongoDb GridFS so that this file can be downloaded to the client at a later stage.
I am planning to do the following
- create a file in memory on the server. let f = new FS.File(wbout);
- add properties (name, size, data, type)
- save file to mongo using a mongo collection
client side example where file is loaded from input control, I can access the same mongo collection on server for inserting a file.
FS.Utility.eachFile(evt, function (file) {
TempDocuments.insert(file, function (err, fileObject) {
if (err) {
console.log('Error: ', err);
}
});
});
How do I go about to create a file from the binary object and save it directly to GridFS?
Do I need to create a file to the fileSystem into a temp location first or can I stream it to gridFS directly?