Node.JS Convert Base64 String to binary and write to MongoDB GridFS

I have a Base64 string that I convert to a binary as follows:

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

I need to insert this into GridFS MongoDB. The problem I am facing is that createReadStream requires a file path in which I already have the file in memory.

This is what I'm trying to do, doesn't 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, he needs a way where I have buf

UPDATE --- I already thought about it ... It 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){})
+4
source share

All Articles