We have some images in our database and want to display their in view. I find two way to do this - the first: we create action method in controller that get an image from database and return FileContentResult:
public ActionResult GetImage( int id )
{
var imageData = ...get bytes from database...
return File( imageData, "image/jpg" );
}
code in view:
<img src='<%= Url.Action( "GetImage", "image", new { id = ViewData["imageID"] } ) %>' />
The second way is to use HttpHandler:
public void ProcessRequest(HttpContext Context)
{
byte [] b = your image...;
Context.Response.ContentType = "image/jpeg";
Context.Response.BinaryWrite(b);
}
and code in view:
<img src="AlbumArt.ashx?imageId=1" />
The first question is what is the most efficient(work more faster) way to implement this functionality (and why it work faster)?
And the second question - is there is a way to put image in our view directly, when we first call action method to return this view? I mean that in action method we get list of images from database and pass their in view as list, and in view use this code:
<%=Html.Image(Model[i])%>
that code must put image into view directly from model.