Binary Get w/ XHTMLHttprequest

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joealey2003@yahoo.com

    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("G ET", "http://127.0.0.1/image.jpg", false);
    xmlHttp.send(nu ll);
    imgbin = xmlHttp.respons eText;

    alert(imgbin.ch arAt(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

    #2
    Re: Binary Get w/ XHTMLHttpreques t



    joealey2003@yah oo.com wrote:

    [color=blue]
    > I need to load an image, access it's content and render it.[/color]

    How about
    var img = document.create Element('img');
    img.src = 'whatever.gif';
    img.alt = 'whatever';
    document.body.a ppendChild(img) ;
    if you need to do it dynamically.
    [color=blue]
    > So, tried:
    >
    > <script type="text/javascript">
    >
    > xmlHttp = new XMLHttpRequest( );
    > xmlHttp.open("G ET", "http://127.0.0.1/image.jpg", false);
    > xmlHttp.send(nu ll);
    > imgbin = xmlHttp.respons eText;[/color]

    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

    Comment

    • McKirahan

      #3
      Re: Binary Get w/ XHTMLHttpreques t

      <joealey2003@ya hoo.com> wrote in message
      news:1105122792 .329287.206750@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      > 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("G ET", "http://127.0.0.1/image.jpg", false);
      > xmlHttp.send(nu ll);
      > imgbin = xmlHttp.respons eText;
      >
      > alert(imgbin.ch arAt(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???
      >[/color]

      Why not just:

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


      Comment

      • joealey2003@yahoo.com

        #4
        Re: Binary Get w/ XHTMLHttpreques t

        Because i need to access it's content first.

        Comment

        • joealey2003@yahoo.com

          #5
          Re: Binary Get w/ XHTMLHttpreques t

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

          Comment

          • Martin Honnen

            #6
            Re: Binary Get w/ XHTMLHttpreques t



            joealey2003@yah oo.com wrote:
            [color=blue]
            > Some texts on internet says it is possible[/color]

            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.ope n('GET', 'kiboInside.gif ', true);
            httpRequest.onr eadystatechange = function () {
            if (httpRequest.re adyState == 4) {
            var status = httpRequest.sta tus + ' ' + httpRequest.sta tusText +
            '\r\n' +
            'length: ' + httpRequest.res ponseText.lengt h + '\r\n' +
            httpRequest.res ponseText;
            alert(status);
            window.imgSourc e = httpRequest.res ponseText;
            var img = document.create Element('img');
            img.src = 'javascript:img Source';
            document.body.a ppendChild(img) ;
            }
            };
            httpRequest.sen d(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

            Comment

            • McKirahan

              #7
              Re: Binary Get w/ XHTMLHttpreques t

              <joealey2003@ya hoo.com> wrote in message
              news:1105124444 .343515.303390@ f14g2000cwb.goo glegroups.com.. .[color=blue]
              > Because i need to access it's content first.[/color]

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


              Comment

              • joealey2003@yahoo.com

                #8
                Re: Binary Get w/ XHTMLHttpreques t

                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");
                }

                Comment

                • McKirahan

                  #9
                  Re: Binary Get w/ XHTMLHttpreques t

                  <joealey2003@ya hoo.com> wrote in message
                  news:1105133040 .757367.8120@z1 4g2000cwz.googl egroups.com...[color=blue]
                  > 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");
                  > }
                  >[/color]

                  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?


                  Comment

                  • joealey2003@yahoo.com

                    #10
                    Re: Binary Get w/ XHTMLHttpreques t

                    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.

                    Comment

                    • Richard Cornford

                      #11
                      Re: Binary Get w/ XHTMLHttpreques t

                      Martin Honnen wrote:
                      <snip>[color=blue]
                      > And IE/Win for instance doesn't support
                      > <img src="javascript :...">[/color]
                      <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\u 0046\u0038\u003 9\u0061\u002b\u 0000"+
                      "\u002b\u0000\u fff0\u0000\u000 0\ufffd\u0000\u 0000"+
                      "\uffff\uffff\u ffff\u0021\ufff 9\u0004\u0001\u 0000"+
                      "\u0000\u0001\u 0000\u002c\u000 0\u0000\u0000\u 0000"+
                      "\u002b\u0000\u 002b\u0000\u004 0\u0002\u0078\u ff8c"+
                      "\uff8f\uff99\u ffa0\uffed\u000 f\u000d\uff8b\u fff4"+
                      "\u004d\u0075\u 0057\uff8d\u001 9\ufff5\ufff3\u 006d"+
                      "\u0055\u0028\u ff96\uff9e\uff8 9\u0036\u0064\u 006a"+
                      "\u0069\uffa7\u 0008\uffc4\ufff 2\u004c\uffd7\u fff6"+
                      "\u006d\u006f\u ffdd\uffce\uff9 6\u006b\u004f\u fff9"+
                      "\u0001\u0021\u ffc2\uffe1\uffa 2\u0066\u0054\u 0029"+
                      "\u0031\uffc6\u ff95\u0013\ufff 8\u0074\u0014\u ff83"+
                      "\u004b\u0057\u ffd2\uff83\uffb c\u0052\uffb5\u 003a"+
                      "\u006e\uffd7\u ffbb\u0005\u001 3\uffc5\u001c\u fff2"+
                      "\uffd8\u002c\u 0045\uff83\u002 e\uffec\uffa9\u ffe9"+
                      "\u0003\uffbf\u ff92\uffe6\uffc 3\u0068\u0035\u 0065"+
                      "\u0067\ufff6\u fff2\u0056\u007 c\u002b\u000d\u 0005"+
                      "\uffa8\uffd7\u 0034\u0008\u000 2\u0016\u0063\u 0028"+
                      "\uffe1\u0056\u ffa7\u0076\uffd 7\u0028\ufff1\u ffd8"+
                      "\u0017\uffb9\u ffa8\u0045\uffa 9\u0050\u0000\u 0000"+
                      "\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.




                      Comment

                      • Martin Honnen

                        #12
                        Re: Binary Get w/ XHTMLHttpreques t



                        Richard Cornford wrote:
                        [color=blue]
                        > Martin Honnen wrote:
                        >[color=green]
                        >>And IE/Win for instance doesn't support
                        >> <img src="javascript :...">[/color]
                        >
                        >
                        > 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.[/color]

                        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

                        Comment

                        • Martin Honnen

                          #13
                          Re: Binary Get w/ XHTMLHttpreques t



                          Martin Honnen wrote:
                          [color=blue]
                          > Richard Cornford wrote:
                          >[color=green]
                          >> IE can
                          >> do javascript: SRCs on images, but only when the image format is XBM.[/color]
                          >
                          > 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.[/color]

                          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

                          Comment

                          • Jim Ley

                            #14
                            Re: Binary Get w/ XHTMLHttpreques t

                            On Sat, 08 Jan 2005 15:22:33 +0100, Martin Honnen <mahotrash@yaho o.de>
                            wrote:
                            [color=blue]
                            >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?[/color]

                            doesn't for me on that combo

                            Jim.

                            Comment

                            • Richard Cornford

                              #15
                              Re: Binary Get w/ XHTMLHttpreques t

                              Jim Ley wrote:[color=blue]
                              > Martin Honnen wrote:
                              >[color=green]
                              >>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?[/color]
                              >
                              > doesn't for me on that combo[/color]

                              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.


                              Comment

                              Working...