[英]How to save a .gif in Classic ASP - converting from PHP script - options for fopen, fwrite
我有一個非常小的php腳本,它完全可以滿足我的需要-但是我需要將其轉換為經典的ASP。 我已經用谷歌搜索,但是無法找到與經典ASP中的“ fopen”或“ fwrite”類似的信息。
我原來的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);
?>
真的很短,很簡單,可以滿足我的需求。 它調用返回gif的API。 我將gif保存在本地服務器上,並且cron-job經常運行腳本以保持gif最新。
我要移到沒有 php的IIS服務器,因此經典的ASP就足夠了。
我已經走了這么遠:
<%
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
%>
我能夠將其與其他在線內容整合在一起。
我只需要弄清楚如何保存將在服務器上返回的gif-然后我將設置計划的任務以使其間隔運行。
任何幫助表示贊賞。
xmlhttp
(IServerXMLHTTPRequest的實例)具有方法responseBody ,該方法返回字節數組,使用它代替responseText
。 然后寫入流並另存為文件。
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
已編輯
首先,在IIS上安裝PHP並不困難,對於您而言,與重寫Classic ASP中的所有內容相比,這可能是一個更好的選擇
定義Response.ContentType很重要。 除此之外,我從未嘗試過使用圖像文件進行此操作,所以我在這里有點猜測
編輯-我已經嘗試過了,而且有效。 將下面的代碼另存為單獨的文件-給其命名為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 %>
然后,可以像嵌入平面圖像一樣使用img標簽嵌入它。
<img src="mygif.asp">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.