Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ServerXMLHTTP to get an image

Status
Not open for further replies.

firsttube

Technical User
Apr 21, 2004
165
CA
I have two asp pages. One page accepts a query string, does some work server side, and then does this to spit out an image:
Code:
Response.ContentType = "image/png"
...some work in here...
Response.BinaryWrite(image)

The other page requests this image from the first page. I'm trying to use the ServerXMLHTTP object but it is not displaying the image:
Code:
url="[URL unfurl="true"]http://theurl.com/thepage.asp?id=1"[/URL]
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.open "GET", url,False
objXmlHttp.send
image = objXmlHttp.responseBody
response.write(image)

All I get on the page is "????"

What is wrong here?

thanks
ft


Information is not Knowledge, Knowledge is not Wisdom, Wisdom is not Truth, Truth is not Beauty, Beauty is not Love, Love is not Music, Music is the best.
 
I notice that you used BinaryWrite on the first page...

If you just open that first page in the browser does it work? If so you can narrow the problem down to the second page... the one without BinaryWrite.
 
Yes, if I open the first page by itself then it works as expected.

The problem is definitely on the second page.

any help would be great
thanks!
ft


Information is not Knowledge, Knowledge is not Wisdom, Wisdom is not Truth, Truth is not Beauty, Beauty is not Love, Love is not Music, Music is the best.
 
Try this (with non error controls etc, so make sure the site is up and working).
[tt]
<%
response.contenttype="image/png"
url="Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.open "GET", url,False
objXmlHttp.send
image = objXmlHttp.responseBody
response.binarywrite image
%>
[/tt]
 
Odd, you shouldn't need an XMLHTTP object on the second page if you just want to display the image in the browser. You should be able to just drop a quick HTML img tag on the page:
Code:
<html>
<body>
<img src="[URL unfurl="true"]http://theurl.com/thepage.asp?id=1"[/URL] />
</body>
</html>

With the XMLHTTP object, both of your pieces of code are doing the same thing, providing raw image data. Granted one of them is generating the image and one of them is opening it remotely, but in the end they are both pushing raw image data as their response.
If I was wrong and this was your intent, then tsuji's solution should work for you, otherwise you just need to output an HTML document with an img tag like I have above.

-T

barcode_1.gif
 
Tarwn, thanks for the info.

Actually, I was doing it the way you described but I wanted to see how this XMLHTTP object worked.

I did get it to work, but I also have some other html and other ASP stuff on the page, and the response.Clear method kills everything else, so the only thing that is displaying is the image.

Is there a way to get the image to display and using ServerXMLHTTP and not affect anything else on the page?

thanks
ft


Information is not Knowledge, Knowledge is not Wisdom, Wisdom is not Truth, Truth is not Beauty, Beauty is not Love, Love is not Music, Music is the best.
 
Getting the image data isn't the problem, outputting it is :)

What your doing is writing the raw file data to the output stream (BinaryWrite) and telling the browser on the other end that this is an image file (ContentType) instead of an HTML document, text file, whatever. So anything you output will be accepted by the end browser as binary image data even if you meant it to be HTML.

Thats way I was saying both scripts were doing the same thing. Either way you need to create a document with an img tag and set the src to one of your two scripts or accept that your used is just going to see the image on a page as if they had gone to
You don't really need the XMLHTTP call since the img tag can link to images by their full url. On the other hand ifyou absolutely must use it, a third script with an img tag that pointed to your XMLHTTP script would work, even if it is a bit roundabout. Another option would be to write an actual file to the server after receiving the data from your XMLHTTP file rather than trying to output directly to the browser. This would let you load the image in, save it on the server, then supply an img tag in your HTML that pointed to your newly saved file.

I admire your cusiosity and desire to learn more about the XMLHTTP object, but I think in this situation it only complicates matters. I definately urge you to play with it more, but as far as making this image display on the page I would personally stick with the img tag pointing to the remote script.

-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top