Read image as bytes from javascript

  • Thread starter thierryParent66
  • Start date
T

thierryParent66

Hi.

I am trying to load image from another domain and reading content.
How an I get the Image array of bytes after loading it.

What I want is to call a server (on another domain as the current
page) with parameters
then parse the image content to receive the server response stored in
the image.

With something like that :

Image = new image()
Image.src = "http://myserver2/myQuery?MyParameter"
---> Parse Image content ...

The server return the Image stream with the reponse (a string) .

Any Idea ?

Thierry
 
R

Richard Cornford

I am trying to load image from another domain and reading
content. How an I get the Image array of bytes after loading it.

You cannot easily get the contents of loaded resources by any other
means that an XML HTTP request, which will not work across domain,
tends to only provide the contents as a string or an XML DOM and that
string is likely the string of 16 bit code points resulting from
interpreting the input as UTF-8 (which probably will not work well
with image data).

Richard.
 
B

Bart Van der Donck

I am trying to load image from another domain and reading content.
How an I get the Image array of bytes after loading it.

What I want is to call a server (on another domain as the current
page) with parameters then parse the image content to receive the
server response stored in the image.

MSIE knows the 'fileSize' property name:

<script type="text/javascript">
IM = new Image()
IM.src = "http://www.apache.org/images/asf_logo_wide.gif"
</script>
<button onClick="if(IM.fileSize) alert(IM.fileSize);">click</button>

Hope this helps,
 
W

Walton

I am trying to load image from another domain and reading content.
How an I get the Image array of bytes after loading it.

What I want is to call a server (on another domain as the current
page) with parameters
then parse the image content to receive the server response stored in
the image.

With something like that :

Image = new image()
Image.src = "http://myserver2/myQuery?MyParameter"
---> Parse Image content ...

The server return the Image stream with the reponse (a string) .

Any Idea ?

Thierry

I do this all the time-

Your problem is that you don't want your servlet (i'm assuming that's
what the "server on another domain" refers to) returning a string, you
want it to return an image. To do that, you will have to set the
response content type of the servlet to something like image/png, then
just write out the byte array (image) from the servlet. all you have
to do then is set the source of an image to that servlet's url - like
you want.
 
W

Walton

The server return the Image stream with the reponse (a string) .

If you don't have control over this server, that makes things much
more difficult. In that case, the only thing I can think of is to
write a servlet proxy (that can see that "other domain" that reads in
the image string and somehow (i don't know how or even if it's
possible) converts it to a byte stream and returns an image. Then,
see my previous post.
 
I

Isaac Schlueter

I am trying to load image from another domain and reading
content.

How an I get the Image array of bytes after loading it.

You can load it into the page, but AFAIK, it's not possible to get the
img bytes. Let us know if you figure out a way to do this. Once the
onload event fires, you can get the height and width, but that's about
it.

You could perhaps use an iframe or a script tag to get the data,
though. If you set the script's "type" tag to something funky before
setting the src, then the browser won't bother parsing it, and you
could then peek at the properties to see if you can get a glimpse at
the code inside. An iframe would work kind of the same way, but might
be even simpler. Just set the display style to "none", set the src,
and then view the response via contentWindow.contentDocument.innerHTML
once the load event has fired.

Or, you know, you could always just write a proxy that sits on your
server and allows you to pipe through via a curl call to specifically
allowed urls. Then you could use XHR like you oughtta ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,472
Messages
2,571,834
Members
48,802
Latest member
shadowoftheunknown
Top