ASP页面静态化批量生成代码分享(多种方法)(2)


end if
sub makeindex()
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
Filen=Server.MapPath("index.htm")
Set Site_Config=FSO.CreateTextFile(Filen,true, False)
Site_Config.Write content
Site_Config.Close
Set Fso = Nothing
Response.Write("<script>alert('已经成功生成首页!')</script>")
end sub
Function getHTTPPage(url)
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
End function
Function BytesToBstr(body,Cset)
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 = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
%>


2、模板分离批量生成

模板文件中要替换的内容均以{...}括起来
为力求简洁,去掉了错误处理代码(replace中要来替换的字符串参数不能为null值,当然fso也应该做错误检查)。
复制代码 代码如下:

<%
' ---------------------------------------------------------------------------------------------------------------------
' 出自: kevin fung http://www.yaotong.cn
' 作者: kevin fung 落伍者ID:kevin2008,转载时请保持原样
' 时间: 2006/07/05落伍者论坛首发
' ----------------------------------------------------------------------------------------------------------------------
Dim start '该变量为指针将要指向的记录集位置,通过参数动态获得
Dim Template '模板文件将以字符串读入该变量
Dim content '替换后的字符串变量
Dim objConn '连接对象
Dim ConnStr '连接字符串
Dim sql '查询语句
Dim cnt:cnt = 1 '本轮循环计数器初始化
start = request("start") '获取本轮指针的开始位置
If IsNumeric(start) Then start = CLng(start) Else start=1
If start=0 Then start = 1 '如果start
ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath("DataBase.mdb")
sql = "select * from table_name"
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open ConnStr
set rs = Server.CreateObject("ADODB.Recordset")
rs.open sql,objConn,1,1 '打开数据集
rs.AbsolutePosition = start '最关键的一步,将指针指向start,start通过参数动态获得
Template = getTemplate(Server.MapPath("template.html"))' template.html为模板文件,通过函数getTemplate读入到字符串,模板文件中要替换的内容均以{...}括起来
While Not rs.eof And cnt<= 500 '500是设定一次请求生成页面的循环次数,根据实际情况修改,如果太高了,记录集很多的时候会出现超时错误

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2332.html