I’m trying to read the image file stored in a MongoDB, to do that I have following code in my ASP.net MVC 3 projects
public FileContentResult ProfileImage(ObjectId id)
{
using (var db = new MongoSession<User>())
{
var user = db.Queryable.Where(p => p.Id == id).FirstOrDefault();
if (user != null && user.ProfilePicture != null)
{
return File(user.ProfilePicture.Content.ToArray(), user.ProfilePicture.ContentType);
}
return null;
}
}
When queried the database I can see the content exist for the ProfilePicture file, but when here in the C# project the content length is 0. Is there anything wrong in the code?
It looks as if you were embedding
GridFileinUser, something likeThat won’t work; at least, not through default behavior.
GridFiles must be treated as first-class citizens, and they must be stored and retrieved using aGridFileCollectionobject:You can find more working sample code in NoRM’s unit tests.
The reason is that, while
GridFileis basically a class like any other, the storage scheme for grid files is very different. While ‘normal’ objects are serialized in-place, and one document always corresponds to one object, this is not true forGridFile.Because files can be much larger than the maximum document size in MongoDB, they’ll be split up into
chunks. Thus, when you store aGridFile, the driver will have to store the grid file and a number of chunks. By default, chunks are 256kB in size, so the number of chunks will depend on the data size. The default collection names arefs.filesandfs.chunks.All this logic resides in the
GridFileCollectionclass, so it won’t be executed if you’re simply storing an embeddedGridFileobject instead of calling theFiles()extension explicitly to retrieve theGridFileCollection.