1

I am using gridfs, so I have in my monDB:

fs.chunks:

{
        "_id" : ObjectId("542e684a8a1cec178a172673"),
        "n" : 1,
        "data" : BinData(0,"2N6DSSfbCN/LLacNDYrJUQDEZgimMUwFpQGoJP0RU19Bi4PM82DjrUnKhE/P9v7P8ZveD1oDpFBM0iml9NE3WQmAvYFoG+nhD73Jm4N9b4LpylaAN5Ef+gVdgGSTAfSUwOikXoVick5pSQCkRmTCU5NT9VVfjHdAx74/ZhFRj+TIRjzlAhzkACBElzgMwGCo7tX+FYrpQLJ5KRmXiwFFwsNtHHzXiK1eu+CG1FumhGpA/qdG8CdDgD1xUHEcerMGO/eLGR9ML7ni/VjXxWzqp2j5DG2/WuKNv7xd3Kz/vr0MctJhuaBIl35YrKhdLnzqDa0uDa6bm4jz+eNyAI2hQbayGo4kVPFe8W7wFpY7qfBvnB9kbocxfZSdADDUNwYaydpT8lIcKEN9XfQJOYZvHp0El"),
        "files_id" : ObjectId("542e684a8a1cec178a172671")
}

fs.files:

{
        "_id" : ObjectId("542e65378axdeckhb0"),
        "uploadDate" : ISODate("2012-11-01"),
        "length" : 15673,
        "chunkSize" : 33222,
        "md5" : "f66e6654854a28e3672cfhds334d223b55a1"
}

I am doing:

var fileId = mongoose.Types.ObjectId(id);
var gridStore = new GridStore(db, fileId, 'r');

gridStore.open(function (err, gridStore) {
   console.log(gridStore.uploadDate);
});

I got :

2012-11-01

BUT I want to display images I tried something like that but it is not clear at all:

 app.get('/data/:imgtag', function(req, res) {

        fileRepository.getFile( function(error,data) {
        res.writeHead('200', {'Content-Type': 'image/png'});
        res.end(data,'binary');
        },    req.params.imgtag
        );
});

app.listen(3000);

Thanks for your help!

Carlos
  • 79
  • 1
  • 4
  • 16
  • 1
    If you `console.log(data)` and the resulting output starts with `2N6DSSfbCN` then you need to change `res.end(data, 'binary')` to `res.end(data, 'base64')` – mscdex Oct 10 '14 at 18:04
  • thanks for your help. But my problem is to do the link between gridstore.open and the app.get – Carlos Oct 11 '14 at 08:14

1 Answers1

0

First, have a look at the GridStore documentation. There are two ways to approach this:

One is to call gridStore.read() inside your gridStore.open() function. You can send the data from callback to the response.

gridStore.read(function(err, data) {
    res.write(data);
    res.end();
});

The data parameter in the callback will contain your file.

Another option is to use gridStore.stream() and pipe the stream to the response.

gridStore.stream().pipe(res);
MForMarlon
  • 865
  • 9
  • 24