Nick Swan
Nick Swan

Reputation: 822

asp.net mvc saving and displaying images in db

how do you go about saving images and displaying them from a SQL Server Image field when using ASP.NET MVC?

Many thanks Nick

Upvotes: 3

Views: 756

Answers (2)

Simon Steele
Simon Steele

Reputation: 11608

You can also do this pretty simply yourself with a controller action:

public void RenderImage(int imageId)
{
    // TODO: Replace this with your API to get the image blob data.
    byte[] data = this.repo.GetImageData(imageId);

    if (data != null)
    {
        // This assumes you're storing JPEG data
        Response.ContentType = "image/jpeg";
        Response.Expires = 0;
        Response.Buffer = true;
        Response.Clear();
        Response.BinaryWrite(data);
    }
    else
    {
        this.ControllerContext.HttpContext.ApplicationInstance.CompleteRequest();
    }
}

Upvotes: 0

Haacked
Haacked

Reputation: 59001

The MvcFutures http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=18459 project has a FileResult which is a type of ActionResult. You could probably use that to return a binary stream to the browser.

Upvotes: 2

Related Questions