views:

286

answers:

5

Hi,

I am getting an image via a post request. How do I show the same on web using javascript. Is it possible to handle binary data using javascript?

A: 

No: images are always loaded from an URL

Maurice Perry
+2  A: 

You might be able to create an img-tag with a base64 src:

<img alt="Embedded Image" 
  src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />

This can probably be done dynamically aswell, using .createElement and setting the src.

but then you need to convert your binary data to base64 using javascript somehow. It would be simpler if you could do that server-side, since handling binary data in javascript might cause you some trouble. That would however give you quite some overhead, but maybe thats not an issue.

Otherwise, see if this helps you: http://www.webtoolkit.info/javascript-base64.html

jishi
+1  A: 

Depending on the data format you might be able to use the data URI to load the image. Note to watch out for browser support.

MattJ
A: 

using Gears you could turn the response into a blob and bind to an URL with the local cache server. after that, any reference to that URL will answer locally with the image data.

Javier
+1  A: 

The most cross-browser way to do this would be to simply fetch the image URL as the response of the POST request, and then make a GET call to the image using img src="..."

Rakesh Pai