Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Binary Get w/ XHTMLHttprequest

5 views
Skip to first unread message

joeal...@yahoo.com

unread,
Jan 7, 2005, 1:33:12 PM1/7/05
to
Hi all...


I need to load an image, access it's content and render it.

So, tried:

<script type="text/javascript">

xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "http://127.0.0.1/image.jpg", false);
xmlHttp.send(null);
imgbin = xmlHttp.responseText;

alert(imgbin.charAt(10)); //just for testing the access
//returns an empty box
</script>

<img src="javascript:imgbin">


Since the javascript code works for html files, i think the problem is
the binary content.

The <img src...> failed too.

Any Hints???

Martin Honnen

unread,
Jan 7, 2005, 1:42:36 PM1/7/05
to

joeal...@yahoo.com wrote:


> I need to load an image, access it's content and render it.

How about
var img = document.createElement('img');
img.src = 'whatever.gif';
img.alt = 'whatever';
document.body.appendChild(img);
if you need to do it dynamically.

> So, tried:
>
> <script type="text/javascript">
>
> xmlHttp = new XMLHttpRequest();
> xmlHttp.open("GET", "http://127.0.0.1/image.jpg", false);
> xmlHttp.send(null);
> imgbin = xmlHttp.responseText;

It is called responseText for a reason.
I think MSXML also has a responseBody property but I don't think
JavaScript can do much with that but passing it on to another method
that can take such an array of bytes as JavaScript itself doesn't know
bytes.

--

Martin Honnen
http://JavaScript.FAQTs.com/

McKirahan

unread,
Jan 7, 2005, 1:53:29 PM1/7/05
to
<joeal...@yahoo.com> wrote in message
news:1105122792.3...@z14g2000cwz.googlegroups.com...

Why not just:

<img src="http://127.0.0.1/image.jpg">


joeal...@yahoo.com

unread,
Jan 7, 2005, 2:00:44 PM1/7/05
to
Because i need to access it's content first.

joeal...@yahoo.com

unread,
Jan 7, 2005, 2:02:37 PM1/7/05
to
Some texts on internet says it is possible but don't show an example,
or a code like mine.

Martin Honnen

unread,
Jan 7, 2005, 2:41:42 PM1/7/05
to

joeal...@yahoo.com wrote:

> Some texts on internet says it is possible

I don't think that the JavaScript string type can deal with binary image
data, if I try the following with Mozilla (where kiboInside.gif is a GIF
image Mozilla can render without problems when directly referenced in
the source attribute of an <img> element) Mozilla fails to render the
dynamically created image correctly, it has the right dimensions but
nothing more:

var httpRequest;
if (typeof XMLHttpRequest != 'undefined') {
httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'kiboInside.gif', true);
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4) {
var status = httpRequest.status + ' ' + httpRequest.statusText +
'\r\n' +
'length: ' + httpRequest.responseText.length + '\r\n' +
httpRequest.responseText;
alert(status);
window.imgSource = httpRequest.responseText;


var img = document.createElement('img');

img.src = 'javascript:imgSource';
document.body.appendChild(img);
}
};
httpRequest.send(null);
}

And the length of the string responseText displayed is not the number of
bytes that Windows shows me for that image file.

And IE/Win for instance doesn't support
<img src="javascript:...">
at all, otherwise the pnglets from
<http://www.elf.org/pnglets/>
would work with IE.

McKirahan

unread,
Jan 7, 2005, 3:09:06 PM1/7/05
to
<joeal...@yahoo.com> wrote in message
news:1105124444.3...@f14g2000cwb.googlegroups.com...

> Because i need to access it's content first.

Why? What do you want to do with binary data?


joeal...@yahoo.com

unread,
Jan 7, 2005, 4:24:00 PM1/7/05
to
I need to check if the image sent was the one that i want.

//algorithm

if(image.data == "data i want"){
show("Yes it is the rigth image");
}
else{
show("Wrong image");
}

McKirahan

unread,
Jan 7, 2005, 4:32:41 PM1/7/05
to
<joeal...@yahoo.com> wrote in message
news:1105133040....@z14g2000cwz.googlegroups.com...

I still don't understand -- but I don't have it as I'm just curious.

1) Wouldn't "image.data" contain binary data?

2) Wouldn't your script have to contain binary data as well for
comparison?


joeal...@yahoo.com

unread,
Jan 7, 2005, 4:47:41 PM1/7/05
to
1) Yes.

2) Supose we have files on Gif, jpg and bmp. How could i check this
files without opening the binary?
I would have to convert it to RGB for example and then check
'imgae[ i ] [ j ]'.
It's a lot of work and would collapses my system.

Richard Cornford

unread,
Jan 8, 2005, 7:19:27 AM1/8/05
to
Martin Honnen wrote:
<snip>

> And IE/Win for instance doesn't support
> <img src="javascript:...">
<snip>

That isn't quite true (though for all practical purposes it is). IE can
do javascript: SRCs on images, but only when the image format is XBM.
XBM being an image format where the data is defined as text, so a long
way form being an efficient form as it takes 5+ characters to define a
byte. It is also a two color format (black and transparent):

var trash = "#define trash_width 16\n#"+
"define trash_height 16\n"+
"static char trash_bits[] = {"+
"0x00,0x01,0xe0,0x0f,"+
"0x10,0x10,0xf8,0x3f,"+
"0x10,0x10,0x50,0x15,"+
"0x50,0x15,0x50,0x15,"+
"0x50,0x15,0x50,0x15,"+
"0x50,0x15,0x50,0x15,"+
"0x50,0x15,0x10,0x10,"+
"0xe0,0x0f,0x00,0x00"+
"};";
...
<img src="javascript:trash" width="16" height="16" alt="Trash Can">

Mozilla is much happier with this general idea and will show
(apparently) any recognised graphic format. E.G.:-

var crossGif =
"\u0047\u0049\u0046\u0038\u0039\u0061\u002b\u0000"+
"\u002b\u0000\ufff0\u0000\u0000\ufffd\u0000\u0000"+
"\uffff\uffff\uffff\u0021\ufff9\u0004\u0001\u0000"+
"\u0000\u0001\u0000\u002c\u0000\u0000\u0000\u0000"+
"\u002b\u0000\u002b\u0000\u0040\u0002\u0078\uff8c"+
"\uff8f\uff99\uffa0\uffed\u000f\u000d\uff8b\ufff4"+
"\u004d\u0075\u0057\uff8d\u0019\ufff5\ufff3\u006d"+
"\u0055\u0028\uff96\uff9e\uff89\u0036\u0064\u006a"+
"\u0069\uffa7\u0008\uffc4\ufff2\u004c\uffd7\ufff6"+
"\u006d\u006f\uffdd\uffce\uff96\u006b\u004f\ufff9"+
"\u0001\u0021\uffc2\uffe1\uffa2\u0066\u0054\u0029"+
"\u0031\uffc6\uff95\u0013\ufff8\u0074\u0014\uff83"+
"\u004b\u0057\uffd2\uff83\uffbc\u0052\uffb5\u003a"+
"\u006e\uffd7\uffbb\u0005\u0013\uffc5\u001c\ufff2"+
"\uffd8\u002c\u0045\uff83\u002e\uffec\uffa9\uffe9"+
"\u0003\uffbf\uff92\uffe6\uffc3\u0068\u0035\u0065"+
"\u0067\ufff6\ufff2\u0056\u007c\u002b\u000d\u0005"+
"\uffa8\uffd7\u0034\u0008\u0002\u0016\u0063\u0028"+
"\uffe1\u0056\uffa7\u0076\uffd7\u0028\ufff1\uffd8"+
"\u0017\uffb9\uffa8\u0045\uffa9\u0050\u0000\u0000"+
"\u003b";
...
<img src="javascript:crossGif" width="43" height="43"
alt="Cross Hairs (red)">

However, it is interesting to observe that the byte data from the
graphic is in the lower byte of the 16 bit Unicode character escapes
(some of which need to be padded, e.g. \uffa8, in order to keep that low
byte negative). I suspect that taking the responseText of an XML HTTP
request (nominally UTF-8) and making a javascript string out of it will
garble any data that was originally binary beyond being useful, even in
Mozilla.

Opera doesn't seem to be interested in javascript: SRCs at all, and is
unlikely to be alone in that.

Richard.


Martin Honnen

unread,
Jan 8, 2005, 8:34:58 AM1/8/05
to

Richard Cornford wrote:

> Martin Honnen wrote:
>
>>And IE/Win for instance doesn't support
>> <img src="javascript:...">
>
>

> That isn't quite true (though for all practical purposes it is). IE can
> do javascript: SRCs on images, but only when the image format is XBM.

Right, I certainly forgot about XBM images when I wrote that. However at
least IE 6 on Windows XP SP 2 doesn't display the XBM for me:
<http://home.arcor.de/martin.honnen/javascript/200501/test2005010801.html>
I am not sure what earlier IE versions did, might run some tests later.

Martin Honnen

unread,
Jan 8, 2005, 9:22:33 AM1/8/05
to

Martin Honnen wrote:

> Richard Cornford wrote:
>
>> IE can
>> do javascript: SRCs on images, but only when the image format is XBM.
>
> Right, I certainly forgot about XBM images when I wrote that. However at
> least IE 6 on Windows XP SP 2 doesn't display the XBM for me:
> <http://home.arcor.de/martin.honnen/javascript/200501/test2005010801.html>
> I am not sure what earlier IE versions did, might run some tests later.

It seems that IE 5, 5.5 can display the XBM provided in a JavaScript
string. Could anyone confirm that IE 6 on Windows XP SP 2 doesn't
display the image?

Jim Ley

unread,
Jan 8, 2005, 9:36:49 AM1/8/05
to
On Sat, 08 Jan 2005 15:22:33 +0100, Martin Honnen <maho...@yahoo.de>
wrote:

>It seems that IE 5, 5.5 can display the XBM provided in a JavaScript
>string. Could anyone confirm that IE 6 on Windows XP SP 2 doesn't
>display the image?

doesn't for me on that combo

Jim.

Richard Cornford

unread,
Jan 8, 2005, 11:21:19 AM1/8/05
to
Jim Ley wrote:

> Martin Honnen wrote:
>
>>It seems that IE 5, 5.5 can display the XBM provided in
>> a JavaScript string. Could anyone confirm that IE 6 on
>> Windows XP SP 2 doesn't display the image?
>
> doesn't for me on that combo

It must be an XP SP 2 thing rather than IE 6 (which displays the XBM
fine for me on other OSs).

Still, I never thought there was much value in the limited and patchy
support for javascript: image SRCs so losing it on modern IE
installations is no great loss.

Richard.


Dag Sunde

unread,
Jan 8, 2005, 11:55:43 AM1/8/05
to
IE 6.0.2800.1106.xpsp2.040919-1003 shows the trashcan


"Martin Honnen" <maho...@yahoo.de> wrote in message
news:41dfecb0$0$1566$9b4e...@newsread4.arcor-online.net...

Jim Ley

unread,
Jan 8, 2005, 12:29:23 PM1/8/05
to
On Sat, 08 Jan 2005 16:55:43 GMT, "Dag Sunde" <m...@dagsunde.com> wrote:

>> It seems that IE 5, 5.5 can display the XBM provided in a JavaScript
>> string. Could anyone confirm that IE 6 on Windows XP SP 2 doesn't
>> display the image?

>IE 6.0.2800.1106.xpsp2.040919-1003 shows the trashcan

IE 6.0.2900.2180.xpsp_sp2_rtm.040803-2158 is mine that doesn't...

Jim.

Randy Webb

unread,
Jan 8, 2005, 6:10:20 PM1/8/05
to

That is the same one I have and no trash can.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq

0 new messages