By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

Assign binary stream to Image object - possible?

P: n/a
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!
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Sergiusz Michalski wrote:
obrazek = new Image(); // OK
obrazek = xmlhttp.responseBody; // this is my dream !!!!! heheh
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);
alert(obrazek.width); //it's null of course,
It should be `undefined'. Maybe you are looking for
`xmlhttp.responseText.length'.
and my dream is that
obrazek.width should equal to real image readed from HTTP stream.
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";
document.getElementById("document_image")=obrazek; //another dream, where
document_image is an image declared in HTML body as <img
id="document_image">
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
}
}
xmlhttp.send(null);


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
Jul 23 '05 #2

P: n/a
Thomas 'PointedEars' Lahn wrote:
What you could try is using the `data' URI scheme:

obrazek.src = "data:" + encodeURI(xmlhttp.responseBody);


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° <ap**************@boogie.bruhaha.de>
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.