471,066 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Read image as bytes from javascript

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

Mar 2 '07 #1
5 28159
On Mar 2, 11:26 am, thierryParen...@gmail.com wrote:
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.
Mar 2 '07 #2
thierryParen...@gmail.com wrote:
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,

--
Bart

Mar 2 '07 #3
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.

Mar 2 '07 #4
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.

Mar 2 '07 #5
On Mar 2, 3:26 am, thierryParen...@gmail.com wrote:
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 ;)

--
Isaac Z. Schlueter
http://isaacschlueter.com

Mar 3 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Thomas | last post: by
3 posts views Thread by nik | last post: by
6 posts views Thread by Francesco Moi | last post: by
70 posts views Thread by quickcur | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.