Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6941003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:49:45+00:00 2026-05-27T12:49:45+00:00

I’m trying to read the image file stored in a MongoDB, to do that

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:49:45+00:00Added an answer on May 27, 2026 at 12:49 pm

    It looks as if you were embedding GridFile in User, something like

    class User 
    { 
        public GridFile ProfilePicture {get; set;}
    }
    

    That 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 a GridFileCollection object:

    var gridFS = conn.Database.Files();
    var file = new GridFile();
    // initialize file
    gridFS.Save(file);
    // query
    var file2 = gridFS.FindOne(new { _id = file.Id });
    // now, you can iterate file2.Content
    

    You can find more working sample code in NoRM’s unit tests.

    The reason is that, while GridFile is 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 for GridFile.

    Because files can be much larger than the maximum document size in MongoDB, they’ll be split up into chunks. Thus, when you store a GridFile, 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 are fs.files and fs.chunks.

    All this logic resides in the GridFileCollection class, so it won’t be executed if you’re simply storing an embedded GridFile object instead of calling the Files() extension explicitly to retrieve the GridFileCollection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.