javascript - Display images from GridFs (MongoDB) - Stack Overflow

I am using the following code to read the image file saved in MongoDB using GridFs:app.get('pictu

I am using the following code to read the image file saved in MongoDB using GridFs:

app.get('/picture', function(req, res) {

 var readstream = gfs.createReadStream({
    filename: 'trooper.jpeg'
 });

 readstream.on('data', function (data) {
  // We got a buffer of data...

 var buf2 = new Buffer(data).toString('base64'); 
 res.send(buf2.toString())
 console.log(buf2.toString());
 console.log(data);
});
  readstream.on('end', function () {
// File finished reading...
});

});

The output of console.log(buff.toString()); is:

dHJvb3Blci5qcGVn

The output of console.log(data); is:

<Buffer 74 72 6f 6f 70 65 72 2e 6a 70 65 67>

To display the image I did this:

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

I am unable to read and display the image in html from GridFs MongoDB

UPDATE:

I have tried this:

app.get('/picture', function(req, res) {
res.contentType('image/jpeg');
var readstream = gfs.createReadStream('trooper.jpeg');
readstream.pipe(res);

});

The output of the above is:

I am using the following code to read the image file saved in MongoDB using GridFs:

app.get('/picture', function(req, res) {

 var readstream = gfs.createReadStream({
    filename: 'trooper.jpeg'
 });

 readstream.on('data', function (data) {
  // We got a buffer of data...

 var buf2 = new Buffer(data).toString('base64'); 
 res.send(buf2.toString())
 console.log(buf2.toString());
 console.log(data);
});
  readstream.on('end', function () {
// File finished reading...
});

});

The output of console.log(buff.toString()); is:

dHJvb3Blci5qcGVn

The output of console.log(data); is:

<Buffer 74 72 6f 6f 70 65 72 2e 6a 70 65 67>

To display the image I did this:

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

I am unable to read and display the image in html from GridFs MongoDB

UPDATE:

I have tried this:

app.get('/picture', function(req, res) {
res.contentType('image/jpeg');
var readstream = gfs.createReadStream('trooper.jpeg');
readstream.pipe(res);

});

The output of the above is:

Share Improve this question edited Jan 21, 2016 at 12:52 Skywalker asked Jan 21, 2016 at 10:29 SkywalkerSkywalker 5,20417 gold badges66 silver badges127 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Unless you really need to embed images into page, use pipes as of saintedlama's answer.

dHJvb3Blci5qcGVn is base64 encoded string "trooper.jpeg". Please ensure you correctly saved binary data to GridFS at the first instance.

You can query it directly and check content of the file stored in the db.

var Grid = require("gridfs-stream");
Grid.mongo = mongo; 

router.get("/:filename", function(req, res){ 
        gfs = Grid(db);
        var readstream = gfs.createReadStream({filename: req.params.filename}); 
        readstream.on("error", function(err){
            res.send("No image found with that title"); 
        });
        readstream.pipe(res);
    });

This is the perfect solution to get images from gridfs

You can just pipe the GridFS read stream to the response without buffering:

app.get('/picture', function(req, res) {
  // Set correct content type first
  res.contentType('image/png');

  fs
    .createReadStream('/tmp/Disneygoofy2012.jpeg')
    .pipe(res);
});

I guess the problem of your buffering solution was that you only pass the first byte package you receive from GridFS to res.send.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745648377a4638108.html

相关推荐

  • javascript - Display images from GridFs (MongoDB) - Stack Overflow

    I am using the following code to read the image file saved in MongoDB using GridFs:app.get('pictu

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信