Im getting a image in a byte array format from the controller, how i can display this in the view? in the simplest way.
Create a controller for displaying images with a Show action that takes the id of the image to display from the database. The action should return a FileResult that contains the image data with the appropriate content type.
public class ImageController
{
public ActionResult Show( int id )
{
var imageData = ...get bytes from database...
return File( imageData, "image/jpg" );
}
}
In your view, construct the image and use the image id to construct a path for the image using the controller and action.
<img src='<%= Url.Action( "show", "image", new { id = ViewData["imageID"] } ) %>' />
Assuming you have a dataRow (dr) with two columns, "name" and "binary_image" (binary_image contains the binary info)
Byte[] bytes = (Byte[])dr["Data"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = dr["Image/JPEG"].ToString();
Response.AddHeader("content-disposition", "attachment;filename=" & dt.Rows[0]["Name"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
I know this post is rather old but it was one of the first that came up when i was trying to figure out how to do this for the most part Augi answer was correct but most of the assemblies are out dated
i download mvc2 preview 1
no need to worry about the microsoft.web.mvc stuff i couldnt find any of that stuff anyway and search for about an hour trying to figure out what it evolved into
this is the code i wrote that works for me for displaying an image from a db field of type image
in my controller class which i called store i have this
public ActionResult GetImage(int id) {
byte[] imageData = storeRepository.ReturnImage(id);
//instead of what augi wrote using the binarystreamresult this was the closest thing i found so i am assuming that this is what it evolved into
return new FileStreamResult(new System.IO.MemoryStream(imageData),"image/jpeg") ;
}
//in my repository class where i have all the methods for accessing data i have this
public byte[] ReturnImage(int id) {
// i tried his way of selecting the right record and preforming the toArray method in the return statment
// but it kept giving me an error about converting linq.binary to byte[] tried a cast that didnt work so i came up with this
byte [] imageData = GetProduct(id).ProductImage.ToArray();
return imageData;
}
now for my view page i tried al kinds of ways i found in these forms and nothing worked i am assuming they were just outdated so i tried on a whim the simplest of all thing i could think of and it worked perfectly
< image src='/store/getimage/<%= Html.Encode(Model.productID) %>' alt="" />"
i kept getting an error from the site about posting img tags so make sure you change the above image to img
hope that helps stop anyone from hunting all day for a current answer
http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=30886