Display Image in GridFS

I am using gridfs-stream https://github.com/aheckmann/gridfs-stream & currently i am on displaying image from gridFS.

When reading data it gives me following output. When i append this data to <img src="data:image/jpeg;base64,(data)">, the image doesn't show.

  gfs
// create a read stream from gfs...
.createReadStream({ filename: 'error1.png' })
// and pipe it to Express' response
.pipe(res);

Output res :- enter image description here

Edited :-

I tried this :-

img.src = 'data:image/jpeg;base64,' + btoa(res);

Output rendered is :-

<img src="data:image/jpeg;base64,W29iamVjdCBPYmplY3Rd">

No image shown.

Answers:

Answer

I use file stream demonstrate:

var rstream = fs.createReadStream('test.png');

var bufs = [];

rstream.on('data', function(chunk) {

    bufs.push(chunk);

}).on('end', function() { // done

    var fbuf = Buffer.concat(bufs);

    var base64 = (fbuf.toString('base64'));

    res.send('<img src="data:image/jpeg;base64,' + base64 + '">');
});

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.