views:

269

answers:

2

I know it is be possible to do this:

<asp:Image runat="server" ImageUrl="~/MyImageHandler.ashx?imageid=2" />

...but I have a case where the byte array data is only available to the Page (ie, not available in the session, and cannot be referenced by an id) so I can't point the ImageUrl to a different page.

Is there a way of giving an asp:Image the byte array to render as an Image?

+1  A: 

The only decent solution to this is to put the byte array in session. If you're concerned about uniqueness (multiple people getting each other's byte arrays), use a random GUID as the key.

SLaks
I'm going to have to give the green tick to this one, just because it would achieve the result, still not in the way I would want it to though - but I'm going to have to conceed that there is no answer to this one
Paul
+2  A: 

The major hurtle you're going to have to deal with is that an <asp:Image/> element gets rendered as a regular <img />, which needs a src attribute that points at a URL.

That being the case, I see two hairy alternatives:

  • Use the technique described here to embed your image encoded in Base64 in the src attribute. Note that this does not work with Internet Explorer.

  • Embed your Base64-encoded image into the page as a hidden <input /> element. You could then use JavaScript to POST that data back to the server, which would just send it back to the browser using Response.Write() (being sure to set the Content-Type appropriately, of course).

Chris Zwiryk
Yes, I had considered the second option there but had to throw it away as it made me feel a little queasy throwing the data back and forth in that way. Good idea though!
Paul
If you're thinking about the second option, you might as well use Session instead (like my answer).
SLaks
Storing the image in the Session is clearly the better alternative (see also: a database). But if, for whatever reason, you don't have access to either of those (and the filesystem...) then this is really your only option.
Chris Zwiryk