简体   繁体   English

如何在Classic ASP中保存.gif-从PHP脚本转换-fopen,fwrite的选项

[英]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. 我有一个非常小的php脚本,它完全可以满足我的需要-但是我需要将其转换为经典的ASP。 I have googled but been unable to find information on anything similar to 'fopen' or 'fwrite' in classic ASP. 我已经用谷歌搜索,但是无法找到与经典ASP中的“ fopen”或“ fwrite”类似的信息。

My original PHP Script is: 我原来的PHP脚本是:

<?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. 它调用返回gif的API。 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. 我将gif保存在本地服务器上,并且cron-job经常运行脚本以保持gif最新。

I'm moving to an IIS server which does not have php on it, so classic ASP will have to suffice. 我要移到没有 php的IIS服务器,因此经典的ASP就足够了。

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. 我只需要弄清楚如何保存将在服务器上返回的gif-然后我将设置计划的任务以使其间隔运行。

Any help appreciated. 任何帮助表示赞赏。

xmlhttp (instance of IServerXMLHTTPRequest) has a method responseBody that returns array of bytes, use it instead of responseText . xmlhttp (IServerXMLHTTPRequest的实例)具有方法responseBody ,该方法返回字节数组,使用它代替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 首先,在IIS上安装PHP并不困难,对于您而言,与重写Classic ASP中的所有内容相比,这可能是一个更好的选择

Defining Response.ContentType is important. 定义Response.ContentType很重要。 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 将下面的代码另存为单独的文件-给其命名为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标签嵌入它。

<img src="mygif.asp">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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