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

  • SEARCH
  • Home
  • 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 7608577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:56:57+00:00 2026-05-31T00:56:57+00:00

I have been through pretty much every post on this site it seems that

  • 0

I have been through pretty much every post on this site it seems that deals with image upload & persistence to a sql server database however, something I’m doing is still not right.

The image is either not saving correctly or pulling from the database correctly as while bytes are being written / retrieved the image is not valid. When comparing the size of the image that was uploaded to the original the size is different (larger)?

Model:

public byte[] Photo { get; set; }
public string PhotoMimeType { get; set; }
public string PhotoName { get; set; }

Sql Server:

[PhotoMimeType] [nvarchar](50) NULL,
[PhotoName] [nvarchar](50) NULL,
[Photo] [image] NULL,

Within a controller I have the following to save an image:

public ActionResult Edit(AgentInfo modifiedAgent, HttpPostedFileBase postedFile)
{
    if(ModelState.IsValid)
    {
        var model = _agentRepository.GetByID(modifiedAgent.AgentID);
        if (TryUpdateModel(model))
        {
            if (postedFile != null)
            {
                int imageLength = postedFile.ContentLength;
                byte[] imgBytes = new byte[imageLength];
                postedFile.InputStream.Read(imgBytes, 0, imageLength);

                model.PhotoMimeType = postedFile.ContentType;
                model.PhotoName = postedFile.FileName;
                model.Photo = imgBytes;
            }
            _agentRepository.Save(model);
            return RedirectToAction("ManageAgents", "Agent");  
        }
    }

    return View("Edit", modifiedAgent);
}

The retrieval:

[HttpGet]
public ActionResult GetImage(int id)
{
    var agent = _agentRepository.GetByID(id);
    return File(agent.Photo, agent.PhotoMimeType, agent.PhotoName);
}

The display:

<img src='@Url.Action("GetImage", "Agent", new { id = Model.AgentID })' />

EDIT:

Well after thinking it was a problem with how I was wiring this all together it turned out to be my database mappings…..frick

Fyi, when mapping with fluent-nhibernate and you want to save image bytes, sql data type is varbinary(max) with the following mapping:

Map(x => x.Photo).Column("Photo").Length(2147483647).CustomSqlType("varbinary(MAX)");
  • 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-31T00:56:59+00:00Added an answer on May 31, 2026 at 12:56 am

    Try rewinding the InputStream just before reading it:

    postedFile.InputStream.Position = 0;
    

    or use a temporary MemoryStream:

    using (var memoryStream = new MemoryStream())
    {
        postedFile.InputStream.CopyTo(memoryStream);
        model.Photo = memoryStream.ToArray();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been through every option to try to find a way to get
I have been looking through Scott Guthrie's MVC tutorials and it seems like the
I have been reading through this wonderful website regarding the recommended Python IDEs and
another Objective-C one for you, probably pretty obvious but I have been at this
hey the title pretty much says it all. i have been trying different methods
EDIT2: the problem seems to be my xsd. it validates pretty much every XML.
I have a simple HTTP server running that pretty much just serves an MP3
I have been through the Apple Developer guides and tutorials and I been through
I have been reading through the C++ FAQ and was curious about the friend
I have been reading through the CodePlex supported open source licenses, i couldn't quite

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.