469,271 Members | 1,743 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Binary Get w/ XHTMLHttprequest

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???

Jul 23 '05 #1
17 5370


jo*********@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/
Jul 23 '05 #2
<jo*********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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???


Why not just:

<img src="http://127.0.0.1/image.jpg">
Jul 23 '05 #3
Because i need to access it's content first.

Jul 23 '05 #4
Some texts on internet says it is possible but don't show an example,
or a code like mine.

Jul 23 '05 #5


jo*********@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.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #6
<jo*********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Because i need to access it's content first.


Why? What do you want to do with binary data?
Jul 23 '05 #7
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");
}

Jul 23 '05 #8
<jo*********@yahoo.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...
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");
}


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?
Jul 23 '05 #9
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.

Jul 23 '05 #10
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.


Jul 23 '05 #11


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
http://JavaScript.FAQTs.com/
Jul 23 '05 #12


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?
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #13
On Sat, 08 Jan 2005 15:22:33 +0100, Martin Honnen <ma*******@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.
Jul 23 '05 #14
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.
Jul 23 '05 #15
IE 6.0.2800.1106.xpsp2.040919-1003 shows the trashcan
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:41**********************@newsread4.arcor-online.net...


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?
--

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

Jul 23 '05 #16
On Sat, 08 Jan 2005 16:55:43 GMT, "Dag Sunde" <me@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.
Jul 23 '05 #17
Jim Ley wrote:
On Sat, 08 Jan 2005 16:55:43 GMT, "Dag Sunde" <me@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...


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

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Christian Stigen Larsen | last post: by
3 posts views Thread by Tron Thomas | last post: by
103 posts views Thread by Steven T. Hatton | last post: by
2 posts views Thread by Lisa Pearlson | last post: by
9 posts views Thread by Ching-Lung | last post: by
3 posts views Thread by John R. Delaney | last post: by
10 posts views Thread by rory | last post: by
16 posts views Thread by Erwin Moller | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.