4

I have a Base64 string that I am converting to binary like this:

 var b64string = req.body.image.substr(23);//The base64 has a imageURL
 var buf = new Buffer(b64string, 'base64');

I need to insert this into MongoDB GridFS. The problem I am having is that createReadStream require a filepath where I already have the file in memory.

This is what I am trying that does not work

var grid = new gfs(db, mongo, 'files');    
grid.createWriteStream(options, function (err, ws) {
            fs.createReadStream(buf, {autoClose: true})
                .pipe(ws)
                .on('close', function (f) {

                    console.log(f._id)
                    res.send(f._id)
                })
                .on('error', function (err) {
                    console.log(err)
                })
        })

But as I described, it wants a path where I have buf

UPDATE --- I was over thinking it... This works

var b64string = req.body.image.substr(23);
    var buf = new Buffer(b64string, 'base64');
    var grid = new Grid(db, 'files');
    grid.put(buf, {}function(err, file){})
Rob
  • 11,185
  • 10
  • 36
  • 54

0 Answers0