Question

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.

Was it helpful?

Solution

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 + '">');
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top