Getting at the binary data in an Image object

D

dirtside

I've search far and wide for an answer, so forgive me if this is a
stupid question. (Well, it probably is.)

Consider the following JavaScript:

var foo = new Image();
foo.src = "http://some.server.com/some_filename.gif";

Now, the browser will retrieve 'some_filename.gif' from some.server.com
and put it into the object foo. What I want to do is then subsequently
analyze the actual binary data contained inside some_filename.gif.
(Specifically, I just need to look at the first few bytes.) Is there
any way to do this in just JavaScript?

Thanks in advance for any assistance.
 
M

McKirahan

dirtside said:
I've search far and wide for an answer, so forgive me if this is a
stupid question. (Well, it probably is.)

Consider the following JavaScript:

var foo = new Image();
foo.src = "http://some.server.com/some_filename.gif";

Now, the browser will retrieve 'some_filename.gif' from some.server.com
and put it into the object foo. What I want to do is then subsequently
analyze the actual binary data contained inside some_filename.gif.
(Specifically, I just need to look at the first few bytes.) Is there
any way to do this in just JavaScript?

Thanks in advance for any assistance.

Will this help?

function XML() {
var sURL = "http://some.server.com/some_filename.gif";
var oXML = new ActiveXObject("Msxml2.XMLHTTP.3.0");
oXML.open("GET",sURL,false);
oXML.send();
return oXML.responseBody;
}

alert( XML() );


You'll see a bunch of question marks but that's the binary data.
Substituting .responseText will show you some readable text ...


You can test with:
var sURL = "http://www.google.com/intl/en/images/logo.gif";

You might have to use one of these instead:
var oXML = new ActiveXObject("Msxml2.XMLHTTP");
var oXML = new ActiveXObject("Microsoft.XMLHTTP");
 
J

Jeff North

| I've search far and wide for an answer, so forgive me if this is a
| stupid question. (Well, it probably is.)
|
| Consider the following JavaScript:
|
| var foo = new Image();
| foo.src = "http://some.server.com/some_filename.gif";
|
| Now, the browser will retrieve 'some_filename.gif' from some.server.com
| and put it into the object foo. What I want to do is then subsequently
| analyze the actual binary data contained inside some_filename.gif.
| (Specifically, I just need to look at the first few bytes.) Is there
| any way to do this in just JavaScript?
|
| Thanks in advance for any assistance.

This is server-side code but you might be able to convert it to
client-side.
http://4guysfromrolla.com/webtech/050300-1.shtml
 
D

dirtside

That looks like IE-specific code... whatever I do has to be
cross-platform. (I tried the above code in Mozilla, and was unsurprised
when the JavaScript debugger said it didn't know what 'ActiveXObject'
was ;)).
 
M

McKirahan

dirtside said:
That looks like IE-specific code... whatever I do has to be
cross-platform. (I tried the above code in Mozilla, and was unsurprised
when the JavaScript debugger said it didn't know what 'ActiveXObject'
was ;)).

Try this.


function getRequestObj() {
var ret = null;
var xml = [
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP"];
if (window.ActiveXObject) {
for (var i=0; i<xml.length; i++) {
try {
ret = new ActiveXObject(xml);
break;
} catch(e) {}
}
} else if(window.XMLHttpRequest) {
try {
ret = new XMLHttpRequest();
} catch(e) {}
}
return ret;
}

var sURL = "http://some.server.com/some_filename.gif";
var oXML = getRequestObj();
oXML.open("GET",sURL,false);
oXML.send();

// if you want the "GIF89" content prefix
alert(oXML.responseText);

// if you want the binary data stream
alert(oXML.responseBody);
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top