views:

19

answers:

1

I'm using ASP.NET MVC and have a model that has System.Data.Linq.Binary property. The property represents a picture that has been stored in the database as an image column.

I am able to use the picture in my pages by setting up a separate controller action and using Response.OutputStream.Write to dump the Binary object and then setting the controller action as an HTML img source.

I'm wondering if there is any way to use a Binary object directly in a view without needing the separate controller action? The idea would be to achieve the below which I know will not work but it demonstrates what I'd like to be able to do.

<img src="<%= Model.MyBinaryProperty%>" />
+1  A: 

By nature of the problem, no.

You can simulate it, but you will always be relying in a separate request that serves the image.

There are just too many options, some:

  • Use a regular asp.net handler
  • Only retrieve on the separate request vs. Store somewhere temporarily and serve from there during the request. Usually the earlier is best
  • Use a controller action
  • Depending on the load characteristics and only if really needed, serve the image download from a different server
  • Setup a route + routehandler that serves the image
  • Setup an action filter that by some convention handles serving the image without needing to explicitly define the separate action method.
  • I'm sure there are others ...
eglasius
I suspect there may be some way to load up a javascript object with the image source or something and save a separate request. But then that means getting tied up in knots of a different kind.
sipwiz