Connecting Tech Pros Worldwide Forums | Help | Site Map

Assign binary stream to Image object - possible?

Sergiusz Michalski
Guest
 
Posts: n/a
#1: Jul 23 '05
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_version >= 5)
var ids = ["Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0",
"Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
for(var i=0; !xmlhttp && i<ids.length; i++) { try { xmlhttp = new
ActiveXObject(ids[i]); } catch(ex) { xmlhttp = false; } }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new
XMLHttpRequest(); }

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

xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) { //downloading complete!
alert(xmlhttp.responseText); // is OK image is loaded type is GIF89a

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

document.getElementById("document_image")=obrazek; //another dream, where
document_image is an image declared in HTML body as <img
id="document_image">

}
}
xmlhttp.send(null);
}


</script>

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


Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Assign binary stream to Image object - possible?


Sergiusz Michalski wrote:
[color=blue]
> obrazek = new Image(); // OK
> obrazek = xmlhttp.responseBody; // 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.responseBody.

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

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

It should be `undefined'. Maybe you are looking for
`xmlhttp.responseText.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.getElementById("document_image")=obrazek; //another dream, where
> document_image is an image declared in HTML body as <img
> id="document_image">[/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(null);[/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/xmmthsendixmlhttprequest.asp>


PointedEars
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#3: Jul 23 '05

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(xmlhttp.responseBody);[/color]

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

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


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


Similar JavaScript / Ajax / DHTML bytes