views:

1130

answers:

5

The situation is that you have to work with an image API and you have to make a POST request to get an image stream that you want to display in the rest of your web page.

I can make an ajax request to that service on page load using jQuery, but i just get a binary stream returned. Is there anyway that JavaScript can take that binary string and display the content type that is in the header?

+2  A: 

I believe you'll need to arrange to have the stream delivered when a URL is referenced by an HTTP GET operation - then have JavaScript set the src attribute of the image to that URL. I've seen this done with ASP.NET, where a .ashx handler is used to stream the image. One then references http://site.com/images/imagehandler.ashx?parameters.

John Saunders
I do not have any control over the how the stream is delivered. I cannot arrange to have this done with a GetOperation.Unless, maybe I could write a proxy that would pretty much be a pass through...
taelor
Such a proxy would work just fine. Just use WebRequest in an async handler.
John Saunders
+1  A: 

Can you not set an image's src attribute to the url you are using for your ajax communication currently? Or do you have to strip out other info from the ajax call first?

Matt Dawdy
the img tag uses a GET request not a POST request. so I don't think the img tag src attribute would work.
taelor
A: 

Just use javascript to create an img element with the url that returns the image as the src:

// jquery
$(#some-id).append('<img src="/get-image/?foo=bar"/>');
vezult
the img tag uses a GET request not a POST request. I wish it was this easy.
taelor
+1  A: 

do form post request with targeting an iframe. this is the only way.

M. Utku ALTINKAYA
yah, I have implemented it using this method. its a sloppy hack, but it does kinda work. i'll give you a +1, but this doesn't answer the question $)
taelor
+2  A: 

I believe what you are looking for is that Data URI Scheme - which allows you to format a really long URI that specifies the needed binary data in itself.

levik
If the resulting stream is to be displayed in 'the rest of the page', however, I fear that it will be much too big to fit in a data:URI...
Martijn
I have seen this, but I don't know how to convert the binary into base64. Do you know of any good blog posts? Does this work with IE6 or even IE7?
taelor