Solved

Display image as part of MVC view.

Posted on 2013-11-29
4
581 Views
Last Modified: 2013-12-02
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" />
            }
0
Comment
Question by:wilfordrocks
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39686463
Use this version of your GetImage() method:

public ActionResult GetImage(
{
    MemoryStream ms = GetImageFromDB();
    return File(ms.ToArray(), "image/png");
}

Open in new window


<img src="/FooController/GetImage" />
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 39686845
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.)
0
 

Author Comment

by:wilfordrocks
ID: 39688161
So simple.  So elegant.  Thank you.
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 39690535
I don't understand you choise of answer. CraigWagner only repeated the code from your question.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now