C# – Display image from database in asp mvc


I'm getting an image in a byte array format from the controller, How can I display this in the view? in the simplest way.

Best Solution

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 : Controller
    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"] } ) %>' />