Display image from database in asp mvc

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="https://stackoverflow.com/questions/880515/<%= Url.Action("show", "image", new { id = ViewData["imageID"] } ) %>" />

Leave a Comment