0

I want to get image from database and display it in the view. image is retrieved but from database but not binding to image. This is my controller code

public ActionResult GetSignatureDetails()
        {

     byte[] image = (from m in objIycEntity.Signatures
                                 where m.SignatureID == 5
                                 select m.Signature1).FirstOrDefault();
                    var stream = new MemoryStream(image.ToArray());
                    return new FileContentResult(image, "image/jpeg");
             }

And this is my view

  <img src='@Url.Action("GetSignatureDetails","Agent")' />

But always image src in firebug is src="/Agent/GetSignatureDetails"

Please can any one help me what I am doing wrong

Chaitanya
  • 95
  • 6
  • 15
  • Take a look here: http://stackoverflow.com/questions/6040920/display-an-image-contained-in-a-byte-with-asp-net-mvc3 – Ric Feb 18 '15 at 11:30
  • Did you try change your return content to " return File(stream.ToArray(), "image/png");" ? – Thiago Custodio Feb 18 '15 at 11:35
  • What do you mean "not binding"? Does the browser's tools show you the image is being downloaded? What other messages does the browser show you? – CodeCaster Feb 18 '15 at 11:57

2 Answers2

-1

have you tried it this way?

    public FileContentResult GetSignatureDetails()
    {
        byte[] image = (from m in objIycEntity.Signatures
                        where m.SignatureID == 5
                        select m.Signature1).FirstOrDefault();
        return File(image, "image/jpeg");
    }
teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • Answering a question consists of understanding the problem, researching a solution and then posting an answer. You skipped the first two steps and you encourage cargo cult programming (you copied the useless `new MemeoryStream()` code, and what does `return new File()` do different from `return new FileContentResult()`, why would that solve the issue, does it even compile?). Post a comment instead. – CodeCaster Feb 18 '15 at 11:58
  • @CodeCaster too much code to comment. But you right, there should not be `new`. I suppose promlem is not in `FileContentResult` part, but in `ActionResult` part. – teo van kot Feb 18 '15 at 12:03
  • The comment would be _"Have you tried `return File(...)` instead of `return new FileContentResult(...)`?"_, which would easily fit in a comment (as demonstrated here). I'm still not sure what you think this will fix, [what do you think `Controller.File()` does internally](https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.file%28v=vs.118%29.aspx)? – CodeCaster Feb 18 '15 at 12:06
  • @CodeCaster same thing, that `new FileContentResult` i suppose. It's just cleaner. – teo van kot Feb 18 '15 at 12:09
  • Cleaner perhaps, but it doesn't do anything different, hence it wouldn't solve OP's problem, which was my point. – CodeCaster Feb 18 '15 at 12:09
  • @CodeCaster why you think so? what about `FileContentResult` or even `FileStreamResult` instead of `ActionResult`, i suppose that's the main problem. – teo van kot Feb 18 '15 at 12:12
  • If that were the issue, OP would have a compile-time error, not an (improperly described) issue at runtime. [All mentioned `...Result` classes inherit from `ActionResult`](https://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult(v=vs.118).aspx). – CodeCaster Feb 18 '15 at 12:36
-1

I give you a sample code hope this will help you:

View :

@model MvcApplication2.Models.AgentProfile
@{
    ViewBag.Title = "imageDemo";
}

<h2>imageDemo</h2>


<img width="100" height="80" src="/Profile/ShowImage/2" alt=""  />

And Controller Code Like this:

 public ActionResult ShowImage(int id)
        {
            if (id>0)
            {
                AgentProfile objag = new AgentProfile();
                MvcApplication2.Models.Image obj = new MvcApplication2.Models.Image();
                obj.ImageID = id;
                DataSet ds = obj.SelectImage();
                objag.ImageSource = (byte[])ds.Tables[0].Rows[0]["ImageContent"];
                //objag.ImageSource = (byte[])row["ImageContent"];
                return File(objag.ImageSource, "image/jpg");
            }

            else
            {
             return   RedirectToAction("Index");
            }  
        }

Thanks.

Aditya
  • 44
  • 1
  • 12