Solved

Display image as part of MVC view.

Posted on 2013-11-29
4
596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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