我正在使用gridfs-stream https://github.com/aheckmann/gridfs-stream&目前我正在显示来自gridFS的图像.
当读取数据时,它给出以下输出.当我将此数据附加到< img src =“ data:image / jpeg; base64,(data)”>时,图像不会显示.
gfs
// create a read stream from gfs...
.createReadStream({ filename: 'error1.png' })
// and pipe it to Express' response
.pipe(res);
输出res:-
编辑:-
我试过了:-
img.src = 'data:image/jpeg;base64,' + btoa(res);
呈现的输出是:-
<img src="data:image/jpeg;base64,W29iamVjdCBPYmplY3Rd">
没有显示图片.
解决方法:
我使用文件流演示:
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 + '">');
});