ASP生成靜態網頁收集整理

生成靜態網頁收集整理 ASP

1.使用FSO生成

<%
'使用FSO生成
Set fs = CreateObject("Scripting.FileSystemObject")
NewFile=Server.MapPath("ud03/fso.htm")
'新建一文件fso.htm,若該文件已存在,則覆蓋它
Set a = fs.CreateTextFile(NewFile, True)
Response.Write"新文件已建立!"
a.close
File=Server.MapPath("ud03/fso.htm")
Set txt=fs.OpenTextFile(File,8,True) '打開成可以在結尾寫入數據的文件
data1="這句話是使用WriteLine方法寫入的。!<Br>"
txt.WriteLine data1
data2="這句話是使用Write方法寫入的。<Br>"
txt.Write data2
txt.Close
%>

2.使用XMLHTTP生成

<%
'使用XMLHTTP生成
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
'把下面的地址替換成你的首頁的文件地址,一定要用http://開頭的絕對路徑,不能寫相對路徑
xml.Open "GET", "http://www.kinoko.name/ud03/", False
xml.Send
BodyText=xml.ResponseBody
BodyText=BytesToBstr(BodyText,"gb2312")
Set xml = Nothing
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile= fso.CreateTextFile(server.MapPath("ud03.htm"), True) '生成的html的文件名
MyFile.WriteLine(BodyText)
MyFile.Close

'使用Adodb.Stream處理二進制數據
Function BytesToBstr(strBody,CodeBase)
    dim objStream
    set objStream = Server.CreateObject("Adodb.Stream")
    objStream.Type = 1
    objStream.Mode =3
    objStream.Open
    objStream.Write strBody
    objStream.Position = 0
    objStream.Type = 2
    objStream.Charset = CodeBase
    BytesToBstr = objStream.ReadText
    objStream.Close
    set objStream = nothing
End Function
%>

3.使用XMLHTTP批量生成

<%
'使用XMLHTTP批量生成
dim strUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp
Html_Temp="<UL>"
For i=1 To 30 '需要生成的id:1到30
Html_Temp = Html_Temp&"<LI>"
Item_Classid = i
FileName = "Archives_"&Item_Classid&".htm" '生成的html文件名
FilePath = Server.MapPath("/")&"/"&FileName
Html_Temp = Html_Temp&FilePath&"</LI>"
Do_Url = "http://www.kinoko.name/ud03/index.php" 'WEB路徑
Do_Url = Do_Url&"?p="&Item_Classid 'WEB路徑之後的ID
strUrl = Do_Url
dim objXmlHttp
set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
objXmlHttp.open "GET",strUrl,false
objXmlHttp.send()
Dim binFileData
binFileData = objXmlHttp.responseBody
Dim objAdoStream
set objAdoStream = Server.CreateObject("ADODB.Stream")
objAdoStream.Type = 1
objAdoStream.Open()
objAdoStream.Write(binFileData)
objAdoStream.SaveToFile FilePath,2
objAdoStream.Close()
Next
Html_Temp = Html_Temp&"<UL>"
%>
<%
Response.Write ( "成功生成文件:" )
Response.Write ( "<BR>" )
Response.Write Html_Temp
%>

 

4.自動按模板生成網站首頁


<%
Response.Expires = 0
Response.expiresabsolute = Now() - 1
Response.addHeader "pragma", "no-cache"
Response.addHeader "cache-control", "private"
Response.CacheControl = "no-cache"
Response.Buffer = True
Response.Clear
Server.ScriptTimeOut=999999999
on error resume next
'***************************************************************
'*                         定義 從模板從讀取首頁 函數
'* 說明:模板文件名爲:index_Template.asp
'***************************************************************
Function GetPage(url)
         Set Retrieval = CreateObject("Microsoft.XMLHTTP")
         With Retrieval
         .Open "Get", url, False, "", ""
         .Send
         GetPage = BytesToBstr(.ResponseBody)
         End With
         Set Retrieval = Nothing
End Function
Function BytesToBstr(body)
         dim objstream
         set objstream = Server.CreateObject("adodb.stream")
         objstream.Type = 1
         objstream.Mode =3
         objstream.Open
         objstream.Write body
         objstream.Position = 0
         objstream.Type = 2
         objstream.Charset = "GB2312"
         BytesToBstr = objstream.ReadText
         objstream.Close
         set objstream = nothing
End Function

'***************************************************************
'* 生頁首頁,文件名爲:default.htm
'***************************************************************
dim Tstr
Tstr = GetPage("http://www.adhome.net/index_Template.asp")
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(".")&"/default.htm")
fout.Write Tstr
fout.close
    Response.write"<script>alert(""生成首頁成功!/n/n文件名爲:default.htm"");location.href="http://www.adhome.net";</script>"
    Response.end
%>

5.將asp頁面轉換成htm頁面

<%
Function GetPage(url)
'獲得文件內容
dim Retrieval
Set Retrieval = CreateObject("Microsoft.XMLHTTP")
With Retrieval
    .Open "Get", url, False ', "", ""
    .Send
    GetPage = BytesToBstr(.ResponseBody)
End With
Set Retrieval = Nothing
End Function
Function BytesToBstr(body)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = "GB2312"
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
on error resume next
Url="http://www.sina.com.cn"'要讀取的頁面地址
response.write "開始更新首頁..."
wstr = GetPage(Url)
'response.write(wstr)
Set fs=Server.CreateObject("Scripting.FileSystemObject")
'if not MyFile.FolderExists(server.MapPath("/html/")) then
'MyFile.CreateFolder(server.MapPath("/html/"))'
'end if
'要存放的頁面地址
dizhi=server.MapPath("index.htm")
If (fs.FileExists(dizhi)) Then
fs.DeleteFile(dizhi)
End If
Set CrFi=fs.CreateTextFile(dizhi)
Crfi.Writeline(wstr)
set CrFi=nothing
set fs=nothing
response.write "...<font color=red>更新完成!</font>"
%>

代碼算是最簡單的,直接保存成一個asp文件即可,只要把URL(要轉化的asp地址)和dizhi(要保存的html地址)設置好就可以了,一般這兩個文件在同一個目錄,才能保證圖片或者css、js起作用。


6.下面是利用XMLHTTP將動態網頁生成靜態網頁的一段簡單代碼。

如一個正常的index.asp頁面,並且用ASP代碼調出數據庫中的內容,另建一個makehtml.asp的頁面,加入一個textarea域,假設爲name="body",將index.asp在textarea裏調出來,如:
<textarea name="body"><!--#include file="index.asp"--></textarea>

將這個textarea包含在表單中,在接收表單頁用創建FSO對象,如下生成index.html文件!

<%
filename="../index.html"
if request("body")<>"" then
set fso = Server.CreateObject("Scripting.FileSystemObject")
set fout = fso.CreateTextFile(server.mappath(""&filename&""))
fout.write request.form("body")
fout.close
set fout=nothing
set fso=nothing
end if
%>

這樣index.html文件就生成了,連模板都用不着,只要將正常情況下使用的ASP文件讀取到textarea裏就可以了,目前尚未發現問題!當然前提是服務器要支持FSO。

開啓FSO權限 在 開始-“運行”中執行regsvr32.exe scrrun.dll即可。如想關閉FSO權限,在上述命令中加/u參數。註冊表中的鍵值位置:HKEY_CLASS_BOOT/F.S.O .FSO中有個方法是CreateFolder,但是這個方法只能在其上一級文件夾存在的情況下創建新的文件夾,所以我就寫了一個自動創建多級文件夾的函數,在生成靜態頁面等方面使用非常方便。函數:

<%
’ --------------------------------
’ 自動創建指定的多級文件夾
’ strPath爲絕對路徑
Function AutoCreateFolder(strPath) ’ As Boolean
         On Error Resume Next
         Dim astrPath, ulngPath, i, strTmpPath
         Dim objFSO
         If InStr(strPath, "/") <=0 Or InStr(strPath, ":") <= 0 Then
                 AutoCreateFolder = False
                 Exit Function
         End If
         Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
         If objFSO.FolderExists(strPath) Then
                 AutoCreateFolder = True
                 Exit Function
         End If
         astrPath = Split(strPath, "/")
         ulngPath = UBound(astrPath)
         strTmpPath = ""
         For i = 0 To ulngPath
                 strTmpPath = strTmpPath & astrPath(i) & "/"
                 If Not objFSO.FolderExists(strTmpPath) Then
                         ’ 創建
                         objFSO.CreateFolder(strTmpPath)
                 End If
         Next
         Set objFSO = Nothing
         If Err = 0 Then
                 AutoCreateFolder = True
         Else
                 AutoCreateFolder = False
         End If
End Function 調用方法:

MyPath = "C:/a/b/c/"
If AutoCreateFolder(MyPath) Then
         Response.Write "創建文件夾成功"
Else
         Response.Write "創建文件夾失敗"
End If
%>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章