Getting at the binary data in an Image object

Discussion in 'Javascript' started by dirtside, Dec 8, 2005.

  1. dirtside

    dirtside Guest

    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.
     
    dirtside, Dec 8, 2005
    #1
    1. Advertising

  2. dirtside

    McKirahan Guest

    "dirtside" <> wrote in message
    news:...
    > 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");
     
    McKirahan, Dec 8, 2005
    #2
    1. Advertising

  3. dirtside

    Jeff North Guest

    On 7 Dec 2005 17:10:37 -0800, in comp.lang.javascript "dirtside"
    <> wrote:

    >| 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
    ---------------------------------------------------------------
    : Remove your pants to reply
    ---------------------------------------------------------------
     
    Jeff North, Dec 8, 2005
    #3
  4. dirtside

    dirtside Guest

    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 ;)).
     
    dirtside, Dec 8, 2005
    #4
  5. dirtside

    McKirahan Guest

    "dirtside" <> wrote in message
    news:...
    > 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);
     
    McKirahan, Dec 8, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denise Smith
    Replies:
    2
    Views:
    665
    Denise Smith
    Nov 22, 2003
  2. =?Utf-8?B?TmF0aGFuVg==?=

    Getting binary image data from SQL Server

    =?Utf-8?B?TmF0aGFuVg==?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    605
    =?Utf-8?B?TWFydGluIFJhbmRhbGw=?=
    Feb 2, 2005
  3. Shlomi
    Replies:
    4
    Views:
    15,143
    Igor N. Kolomiyets
    Feb 2, 2004
  4. Jack
    Replies:
    2
    Views:
    230
    John W. Krahn
    Jan 25, 2008
  5. bjarthur
    Replies:
    1
    Views:
    227
    Randell D.
    Feb 23, 2005
Loading...

Share This Page