0

So I've successfully downloaded my image file to my MongoDB using Multer and Multer-Gridfs-Storage, but I'm having trouble retrieving it.

When I tried to retrieve the data using GridFS-Stream, it came back like this previous question: GridFS : How to display the result of readstream.pipe(res) in an <img/> tag?

When I use this code, what's sent to my Front-End is only the first chunk in the collection, but it's actually usable.

const readstream = gfs.createReadStream({ filename: files[0].filename });

readstream.on('data', (chunk) => {
    res.send({ image: chunk.toString('base64') })              
})

How am I able to get back all of the chunks? Should I give up and start using GridFSBucket?

  • [GridFS](https://docs.mongodb.com/manual/core/gridfs/) is a driver mechanism for storing **large chunks of data** ( being greater than the 16MB limit for BSON Documents ) by spreading that data over multiple documents. If you think you want to return an image stored using GridFS as a base64 string, then you are doing several things wrong. Pretty certain none of your images exceed 16MB. And if they did, then you [would not base64 encode them.](https://stackoverflow.com/questions/4715415/base64-what-is-the-worst-possible-increase-in-space-usage) – Neil Lunn Oct 05 '19 at 04:42

1 Answers1

0

I ended up trying this and it work!

            let data = ''
            readstream.on('data', (chunk) => {
                data += chunk.toString('base64')
            })
            readstream.on('end', () => {
                res.send(data)
            })