Solved

Display image as part of MVC view.

Posted on 2013-11-29
4
599 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 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