Hi all,
how do you go about saving images and displaying them from a SQL Server Image field when using ASP.NET MVC?
Many thanks Nick
Hi all,
how do you go about saving images and displaying them from a SQL Server Image field when using ASP.NET MVC?
Many thanks Nick
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.
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();
}
}
The method above works but seems to be expensive to me. This forces you to make 2 calls to the controller. One to display the non-image data and the other to display the image. Is there a way to do this in one shot?