I have schema that take two fields - name
and file
.
file
is Binary
type and name
is a String
I use this schema to save images as documents in my db
While the image is getting saved without issues - I am not sure how can I read it later. The main propose is to be able to search a file by its name using findOne
and then to stream it to the response using pipe
or any other solution that might work - to show the image to the client inside <img/>
tag. I tried to convert the file to base64
and then to send something like res.send('data:image/png;base64,${file.file}')
without much luck .
* Note that because I have inside the document two fields (name
and file
) I need to stream only the file
for obvious reasons
This is my GET request to fetch the file :
router.get('/:url',(req, res) => {
const url = req.params.url;
console.log(url)
console.log('streaming file')
File.
findOne({ name:url}, 'file').
cursor().
on('data', (doc) => {
console.log('doc', doc.file);
res.send(doc.file)
}).
on('end', () => { console.log('Done!'); });
})
this not helps because its for streaming file with path which I dont have. My file stored in a db