Solved

Display image as part of MVC view.

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

776 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