I am trying to display the images in my Asp.Net MVC 1.0 application.
I can successfully get the Image (into byte[]) from DB.
How can I display it into the <img>?
I am trying to display the images in my Asp.Net MVC 1.0 application.
I can successfully get the Image (into byte[]) from DB.
How can I display it into the <img>?
Return a FileResult from action method:
return File(imageData, "image/png");
Note that outputting HTML of the page and the image should be done in two separate requests. You have to generate a URL for the src attribute to the action that returns the image and in that action, you can output the image contents.
This works (tested):
<img src="<%= Url.Action("ShowImage", "Image", new { Id = imageId }) %>" />
Says Microsoft, and they have every reason to back SQL. It's fast on your development machine, but it doesn't scale.
It's better to store image paths in SQL instead, and store the image files themselves on the filesystem.
If you already have images in your DB and can't migrate, disk caching can eliminate the overhead.
DISCLAIMER: I'm the author of this software
The free ImageResizing.Net library has an (AGPL/commercial) SqlReader plugin, which supports any database schema, and can serve images from SQL as efficiently as is possible. Add the disk caching plugin, and you'll be back to filesystem-level performance and scalability with minimal development effort.