I am new to mongodb.
I stored binary data with below code snippet:
var data = fs.readFileSync(path);
var image = new mongodb.Binary(data);
//open connection/collection
var record = {picname: id, content: image };
collection.save(record, {safe: true}, function(err,result){
if(err)
console.log(err.stack);
});//save
I can see the record size in db. there is binary data. record size also matched with file size. am happy.
Now, retrieved same binary data from mongo and trying to send it in response:
var record = {picname: id};
collection.findOne(record, function(err,result){
if(err)
console.log(err.stack);
else
{
console.log('before read from db for download.');
//HOW TO READ IMAGE/BINARY DATA FROM RESULT?
//I need to send result in response. Any Idea?
console.log('before read from db for download');
}
});
I am sending binary data with below code snippet. It's not working for all the files. What could be the issue:
collection.findOne(record, function(err,result){
if(err)
console.log(err.stack);
else
{
console.log('before read from db for download. result: [' + result.picname + " ], type: " + result.imagetype);
res.end(result.content.buffer, "binary");
console.log('Responded SUCCESS: ' + id );
}
});//findOne
Please let me know how to retrieve and send via response.
Thanks in advance DD.
file.stream(true).pipe(res);