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

Having problem to get GIF file through XMLHttpRequest

P: n/a
hi dear committee here,
Recently I am trying to writing a widget with will request a gif file
from the server. I have done most of the work and the server now is
sending back the gif file with the following header:

Server: Microsoft-IIS/5.0 Cache-Control: private Date: Fri, 23 Sep 2005
13:16:29 GMT Content-Length: 154 Content-Type: image/gif Via: 1.1
zproxy (NetCache NetApp/5.5R3D2)

However, when I use the code below to set the image to correscponding
element, it just doesn't show:
//code to set gif to image element
document.getElementById("spamcode").src = req.responseText;

I have tried to view the responseText in a div area using the
following code:
//show content of sent back gif file
document.getElementById("indicator").innerHTML = req. responseText();

and it simply give me a string like this:
GIF89a !,
k"(D%JQDJ(DB"Q"DJ(P@%B"Q" D!J,,(P@J(Q
D! P"DJ(Q@% "Q"D!JPDU;

I am quite new to javascript and just get my hand on the
XMLHttpRequest. Tried a lot of method like:
document.getElementById("spamcode").src =
"data:image/gif,base64;"+req.responseText;
But still, I am no lucky.

I am not sure whether it is because of the decoding problem because I
searched the web and the GIF89a file content always shows like
GIF89a\1\0\1\0\200[...]\1\0;

Hope you exports could give me some way to solve this problem,
anyway, school final exam is coming..

regards,
Liao Liang
Nanyang Technological University

Sep 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a


willie wrote:

However, when I use the code below to set the image to correscponding
element, it just doesn't show:
//code to set gif to image element
document.getElementById("spamcode").src = req.responseText;


Frankly I would forget about XMLHttpRequest and simply do
document.getElementById("spamcode").src = 'whatever.gif'
or
document.getElementById("spamcode").src =
'serverSideAppGeneratingImage.php'

There is no need to use XMLHttpRequest to have the browser render some
image data with an <img> element.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 23 '05 #2

P: n/a
willie wrote:
<snip>
//code to set gif to image element
document.getElementById("spamcode").src = req.responseText;

I have tried to view the responseText in a div area using the
following code:
//show content of sent back gif file
document.getElementById("indicator").innerHTML = req. responseText();

and it simply give me a string like this:
GIF89a !,
k"(D%JQDJ(DB"Q"DJ(P@%B"Q" D!J,,(P@J(Q
D! P"DJ(Q@% "Q"D!JPDU;


That my friend would be the actual data inside the gif file.

What the .src attribute in an <img> element is referring to
is the URL of the image, not the image data itself.

I gather you must already program in another language,
as getting to the result you have here is typically
considered to be a more advanced programming topic.


--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Sep 23 '05 #3

P: n/a
Hi, thanks for your reply, there is a critica that I have to use
XMLHttpRequest - have to send back the header information I gathered
previously ( which contains the required cookie info or the server
simply send back a error page ) together with the image request.

And also, I am doing this in Apple's Dashboard enviroment, it most
likely will not store the cookie for me and send it like a browser did,
I have to store the cookie info in a variable and manuly transfer to
the server using XMLHttpRequest's header method.

and this is what I did:
1. XMLHttpReqest to a index page ->get header which contains certian
cookie and store it into a variable
2. make another XMLHttpRequest to another page generating GIF ( require
cookie info ) -> I send back the cookie info with setRequestHeader
method
3. Now I am trying to get the content sent back from the server -> the
problem occurs.

That's the most part make me painful...is there any way to parse the
raw data into gif image?

regards
Liao Liang
Nanyang Technological University

Sep 24 '05 #4

P: n/a


willie wrote:
That's the most part make me painful...is there any way to parse the
raw data into gif image?


Mozilla used to support HTML
<img src="javascript: scriptExpressionReturningImageData">
respectively script
imgElement.src = 'javascript: scriptExpressionReturningImageData'
see
<http://www.elf.org/colorcube/index.html>
<http://www.elf.org/pnglets/>

As you are talking about Apple I guess you want to have that work with
Safari or the Safari rendering engine, I have no idea whether Safari
supports that.

And even if the javascript: pseudo URL src is supported I have doubts
that you get binary image data into responseText. We head a thread about
that some while ago, see
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/7bdb401f581141d0/94e89601ec14e473?lnk=st&q=responseText+binary+imag e+data+group:comp.lang.javascript&rnum=1&hl=en#94e 89601ec14e473>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 24 '05 #5

P: n/a
Martin Honnen wrote:
Mozilla used to support HTML
<img src="javascript: scriptExpressionReturningImageData">
respectively script
imgElement.src = 'javascript: scriptExpressionReturningImageData'
see
<http://www.elf.org/colorcube/index.html>
<http://www.elf.org/pnglets/>


I really miss that feature. I have an XBM icon editor I wtote
that worked in IE and NS which was a real handy tool,
untill XBM went away :(

[Gets all misty eyed]

With the advent of XMLHTTP I'm contemplating what I might get
away with today.

--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> Internet Programming since 1994 <>=-- DHTML NSAPI TCP/IP
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Sep 24 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.