Assign binary stream to Image object - possible?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sergiusz Michalski

    Assign binary stream to Image object - possible?

    Hallo!!

    According to my previous post with popups hanging, now I'm trying to bypass
    this strange behaviour using loading JavaScript image not by image.src=URL
    but straight away from stream using HTTP GET method.
    The problem is, how to assign stream from downloaded image (ex. GIF89a) to a
    JavaScript Image object ?

    Below is my code with explanation what I'm trying to do:

    <script type="text/javascript">

    var xmlhttp;
    /*@cc_on @*/
    /*@if (@_jscript_vers ion >= 5)
    var ids = ["Msxml2.XMLHTTP .5.0","Msxml2.X MLHTTP.4.0",
    "Msxml2.XMLHTTP .3.0","Msxml2.X MLHTTP", "Microsoft.XMLH TTP"];
    for(var i=0; !xmlhttp && i<ids.length; i++) { try { xmlhttp = new
    ActiveXObject(i ds[i]); } catch(ex) { xmlhttp = false; } }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest! ='undefined') { xmlhttp = new
    XMLHttpRequest( ); }

    if (xmlhttp) {
    xmlhttp.open("G ET","http://test2/load?BINARY_ID= 42311183",true) ; //zczytuje
    obrazek

    xmlhttp.onready statechange=fun ction() {
    if (xmlhttp.readyS tate==4) { //downloading complete!
    alert(xmlhttp.r esponseText); // is OK image is loaded type is GIF89a

    obrazek = new Image(); // OK
    obrazek = xmlhttp.respons eBody; // this is my dream !!!!! heheh
    alert(obrazek.w idth); //it's null of course, and my dream is that
    obrazek.width should equal to real image readed from HTTP stream.

    document.getEle mentById("docum ent_image")=obr azek; //another dream, where
    document_image is an image declared in HTML body as <img
    id="document_im age">

    }
    }
    xmlhttp.send(nu ll);
    }


    </script>

    Thanks for any help, solution, info!!!
    Serge!


  • Thomas 'PointedEars' Lahn

    #2
    Re: Assign binary stream to Image object - possible?

    Sergiusz Michalski wrote:
    [color=blue]
    > obrazek = new Image(); // OK
    > obrazek = xmlhttp.respons eBody; // this is my dream !!!!! heheh[/color]

    Your weird writing style aside (and I don't mean the script code), this
    is not your dream as you would overwrite the previously assigned object
    reference `obrazek' with the value of xmlhttp.respons eBody.

    What you could try is using the `data' URI scheme:

    obrazek.src = "data:" + encodeURI(xmlht tp.responseBody );
    [color=blue]
    > alert(obrazek.w idth); //it's null of course,[/color]

    It should be `undefined'. Maybe you are looking for
    `xmlhttp.respon seText.length'.
    [color=blue]
    > and my dream is that
    > obrazek.width should equal to real image readed from HTTP stream.[/color]

    It cannot, since what you retrieve is a string. However, if you decode the
    string, you may find out the original width of the image coded with it. Or
    try

    var img = new Image();
    img.onload = new Function("alert (this.width);") ;
    img.src = "anyURL";
    [color=blue]
    > document.getEle mentById("docum ent_image")=obr azek; //another dream, where
    > document_image is an image declared in HTML body as <img
    > id="document_im age">[/color]

    Use a `data' URI and the `src' property, see above. If you use names
    instead of IDs, you may use the more performant and compliant

    document.images["document_image "].src
    [color=blue]
    > }
    > }
    > xmlhttp.send(nu ll);[/color]

    The method does not require an argument. Sending `null' is likely to be
    equal to sending nothing:

    <http://msdn.microsoft. com/library/default.asp?url =/library/en-us/xmlsdk/html/xmmthsendixmlht tprequest.asp>


    PointedEars

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Assign binary stream to Image object - possible?

      Thomas 'PointedEars' Lahn wrote:
      [color=blue]
      > What you could try is using the `data' URI scheme:
      >
      > obrazek.src = "data:" + encodeURI(xmlht tp.responseBody );[/color]

      Err, should contain a MIME type and evaluate `responseText', for example:

      obrazek.src = "data:image/png," + encodeURI(xmlht tp.responseText );


      PointedEars
      --
      Fremd ist der Fremde nur in der Fremde.
      -- Dieter Brügmann in dag° <ap97mm.3vsmp25 .1@boogie.bruha ha.de>

      Comment

      Working...