Link to home
Start Free TrialLog in
Avatar of wilfordrocks
wilfordrocks

asked on

Display image as part of MVC view.

My goal is to display an image from the database on the SAME view as other information, not in a second browser window.

This method gets an image but returns it to a new browser window.
Controller:
  public ActionResult GetImage(
  {
        MemoryStream ms = GetImageFromDB();
        return File(ms.ToArray(), "image/png");
   }

If I change the method to store the image in the model, like so
Model:
public FileContentResult imageContent { get; set; }

Controller:
public void GetImage(
  {
        MemoryStream ms = GetImageFromDB();
         alphaIndexModel.imageContent = File(ms.ToArray(), "image/png");
   }

View:
How can I get it to display in the view?  This code does not work.
@if(@Model.imageContent != null)
            {
            <img src="@Model.imageContent.FileContents" style="" height="800" width="1200" />
            }
ASKER CERTIFIED SOLUTION
Avatar of Craig Wagner
Craig Wagner
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To send the image data in the same view, you would have to base64-encode it and embed it in the image source URL.

Just show the image the normal way, let the browser request the image from your `GetImage` resource:

<img src="/Controller/GetImage" height="800" width="1200" />

Open in new window


(Adjust the URL to whatever controller name you use.)
Avatar of wilfordrocks
wilfordrocks

ASKER

So simple.  So elegant.  Thank you.
I don't understand you choise of answer. CraigWagner only repeated the code from your question.