从远程图片保存到动态生成HTML页面:
我们先看这样一个例子:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<%
Set m_XMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
m_XMLHTTP.Open "get","http://localhost/images/apple.jpg",false,"",""
m_XMLHTTP.Send
m_data =m_XMLHTTP.ResponseBody
Set m_file = Server.CreateObject("Adodb.Stream")
m_file.Type = 1
m_file.open
m_file.Write m_data
m_file.SaveToFile Server.MapPath("/test.jpg"), 2
m_file.Cancel
m_file.Close
set m_file=nothing
set m_XMLHTTP=Nothing
%>
<body>
</body>
</html>
这里,我们使用了Microsoft.XMLHTTP组件,该组件可以让我们请求HTTP服务器。如果该服务器提供了XML数据服务,那么我们可以向该服务器取得XML数据。而这里,我们只向本地服务器请求了一张图片,然后用Adodb.Stream保存到了本地的根目录下。这样,我们就完成了远程图片保存到本在的任务。
而对于在线编辑器里的图片,我们可以这样保存:
Function eWebEditor_ReplaceRemoteUrl(sHTML, sSavePath, sExt)
Dim s_Content
s_Content = sHTML
If eWebEditor_IsObjInstalled("Microsoft.XMLHTTP") = False then
eWebEditor_ReplaceRemoteUrl = s_Content
Exit Function
End If
If sSavePath ="" Then sSavePath = "UploadFile/"
If sExt = "" Then sExt = "jpg|gif|bmp|png"
Dim re, RemoteFile, RemoteFileurl, SaveFileName, SaveFileType, ranNum
Set re = new RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "((http|https|ftp|rtsp|mms):(\/\/|\\\\){1}((\w)+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(\S*\/)((\S)+[.]{1}(" & sExt & ")))"
Set RemoteFile = re.Execute(s_Content)
For Each RemoteFileurl in RemoteFile
SaveFileType = Mid(RemoteFileurl, InstrRev(RemoteFileurl, ".") + 1)
Randomize
ranNum = Int(900 * Rnd) + 100
SaveFileName = sSavePath & year(now) & month(now) & day(now) & hour(now) & minute(now) & second(now) & ranNum & "." & SaveFileType
Call eWebEditor_SaveRemoteFile(SaveFileName, RemoteFileurl)
s_Content = Replace(s_Content,RemoteFileurl,SaveFileName)
Next
eWebEditor_ReplaceRemoteUrl = s_Content
End Function
Sub eWebEditor_SaveRemoteFile(s_LocalFileName,s_RemoteFileUrl)
Dim Ads, Retrieval, GetRemoteData
On Error Resume Next
Set Retrieval = Server.CreateObject("Microsoft.XMLHTTP")
With Retrieval
.Open "Get", s_RemoteFileUrl, False, "", ""
.Send
GetRemoteData = .ResponseBody
End With
Set Retrieval = Nothing
Set Ads = Server.CreateObject("Adodb.Stream")
With Ads
.Type = 1
.Open
.Write GetRemoteData
.SaveToFile Server.MapPath(s_LocalFileName), 2
.Cancel()
.Close()
End With
Set Ads=nothing
End Sub
当我们用一段含有HTTP请求的文本来调用eWebEditor_ReplaceRemoteUrl时,就可以将里面的所有图片(jpg|gif|bmp|png)保存到本地服务器。
由此我们再展开:如果我请求的不是图片,而是一个ASP或其它的动态页面,而且保存的也不是图片,而是HTML文件,那么我们可以这样来做:
<%
Set m_XMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
m_XMLHTTP.Open "get","http://localhost/ViewInfo.asp?id=12",false,"",""
m_XMLHTTP.Send
m_data =m_XMLHTTP.ResponseBody
Set m_file = Server.CreateObject("Adodb.Stream")
m_file.Type = 1
m_file.open
m_file.Write m_data
m_file.SaveToFile Server.MapPath("/test.htm"), 2
m_file.Cancel
m_file.Close
set m_file=nothing
set m_XMLHTTP=Nothing
%>
这时,我们就生成了一个HTML页面,而它的内容与我们请求ViewInfo.asp?id=12时所得到的完全一样。
如果我们在后台管理中,在数据库里加一个字段,也就是文件保存的文件名,那么我们就得到一个静态的HTML文件了,而该文件可以是一条新闻。
其实这里我们用的到的核心就是Microsoft.XMLHTTP和Adodb.Stream,这里就对这两个做一个简单的说明:
==========================================
Microsoft.XMLHTTP
Properties
onreadystatechange* Specifies the event handler to be called when the readyState property changes. Read/write.
readyState Represents the state of the request. Read-only.
responseBody Represents the response entity body as an array of unsigned bytes. Read-only.
responseStream Represents the response entity body as an IStream. Read-only.
responseText Represents the response entity body as a string. Read-only.
responseXML Represents the response entity body as parsed by the Microsoft? XML Parser (MSXML). Read-only.
status Represents the HTTP status code returned by a request. Read-only.
statusText Represents the HTTP response line status. Read-only.
* denotes an extension to the Worldwide Web Consortium (W3C) Document Object Model (DOM).
Methods
abort Cancels the current HTTP request.
getAllResponseHeaders Retrieves the values of all the HTTP headers.
getResponseHeader Retrieves the value of an HTTP header from the response body.
open Initializes an MSXML2.XMLHTTP request, and specifies the method, URL, and authentication information for the request.
send Sends an HTTP request to the server and receives a response.
setRequestHeader Specifies the name of an HTTP header.
========================================================
Adodb.Stream
Stream Object Properties, Methods, and Events
Properties
Charset Property
EOS Property
LineSeparator Property
Mode Property
Position Property
Size Property (ADO Stream)
State Property
Type Property (ADO Stream)
Methods
Cancel Method
Close Method
CopyTo Method
Flush Method
LoadFromFile Method
Open Method (ADO Stream)
Read Method
ReadText Method
SaveToFile Method
SetEOS Method
SkipLine Method
Stat Method
Write Method
WriteText Method
更多的信息可以在MS的官方网站上找一些技术文章。
文章来源:http://computer.mblogger.cn/wucountry/posts/35329.aspx