简体   繁体   中英

How to save a .gif in Classic ASP - converting from PHP script - options for fopen, fwrite

I have this very tiny php script which does exactly what I need - I need to convert it to classic ASP however. I have googled but been unable to find information on anything similar to 'fopen' or 'fwrite' in classic ASP.

My original PHP Script is:

<?php
$responseImg = file_get_contents("http://url.to/the/api/thatreturnsagif");
$fp = fopen("/my/server/public_html/mydirectory/samepicture.gif", "w");
fwrite($fp, $responseImg);
fclose($fp);
?>

Really short, really simple and does just what I need. It makes a call to an API that returns a gif. I save the gif on my local server and a cron-job runs the script every so often to keep the gif up to date.

I'm moving to an IIS server which does not have php on it, so classic ASP will have to suffice.

I've gotten this far:

<% 
url = "http://url.to/the/api/thatreturnsagif"
set xmlhttp = server.CreateObject("Msxml2.ServerXMLHTTP.6.0") 
xmlhttp.open "GET", url, false 
xmlhttp.send "" 
Response.write xmlhttp.responseText 
set xmlhttp = nothing 
%>

I was able to put that together from some other stuff online.

I just need to figure out how to save the gif that will be returned on the server - then I will setup scheduled tasks to run it on an interval.

Any help appreciated.

xmlhttp (instance of IServerXMLHTTPRequest) has a method responseBody that returns array of bytes, use it instead of responseText . Then write into a stream and save as file.

url = "http://url.to/the/api/thatreturnsagif"
set xmlhttp = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0") 
xmlhttp.open "GET", url, false 
xmlhttp.send
With Server.CreateObject("Adodb.Stream")
    .Type = 1 '1 for binary stream
    .Open
    .Write xmlhttp.responseBody
    .SaveToFile Server.Mappath("\mydirectory\samepicture.gif"), 2 ' 2 for overwrite
    .Close
End With
set xmlhttp = nothing 

EDITED

First of all, installing PHP on IIS isn't that difficult, it might be a better option for you than rewriting everything in Classic ASP

Defining Response.ContentType is important. Other than that I've never tried this with an image file before so I'm guessing a bit here

Edited - I've tried this and it works. Save the code below as a separate file - give it a name like mygif.asp

<% url = "http://url.to/the/api/thatreturnsagif"       
Set mygif = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0")
    mygif.open "GET",url, false
    mygif.send 
    Response.ContentType = "image/gif" 
    Response.binarywrite mygif.ResponseBody

set mygif=nothing %>

Then you can embed it with an img tag just as you would embed a flat image.

<img src="mygif.asp">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM