2005-11-11 18:28:10 by: h4x0r

ASP生成静态htm页面基本代码

Font Size: Large | Medium | Small
以下为mk.asp文件的具体代码:
<%
filename="test.htm"
if request("body")<>"" then
set fso = Server.createObject("Scripting.FileSystemObject")
set fout = fso.createTextFile(server.mappath(""&filename&""))
fout.write "标题(title):" & request.form("title") & "<br>"
fout.write "内容(body):" & request.form("body")
fout.close
set fout=nothing
set fso=nothing
end if
%>
<form name="form1" method="post" action="">
<input name="title" size=18><br>
<textarea name="body"></textarea>
<br>
<br>
<input type="submit" name="Submit" value="生成">
</form>
然后在浏览器输入mk.asp这个页面,填写完毕后,就会生成test.htm这个文件,里面的内容就是你刚才填写的。

下面的例子是将、index.asp?id=1/index.asp?id=2/index.asp?id=3/这三个动态页面,分别生成ndex1.htm,index2.htm,index3.htm存在根目录下面:
<%
dim strUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp
Html_Temp="<UL>"
For i=1 To 3
Html_Temp = Html_Temp&"<LI>"
Item_Classid = i
FileName = "Index"&Item_Classid&".htm"
FilePath = Server.MapPath("/")&"\"&FileName
Html_Temp = Html_Temp&FilePath&"</LI>"
Do_Url = "http://"
Do_Url = Do_Url&Request.ServerVariables("SERVER_NAME")&"/main/index.asp"
Do_Url = Do_Url&"?Item_Classid="&Item_Classid
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
%>

这里我引用一个简单实例:(旅游电子商务)全国各大城市酒店应用静态页(htm)分页显示

1.应用系统环境:win2000+ASP+MSSQL/ACCESS(数据库基本没有关系了通用的)+iis5.0

2.1个城市列表(CityHtml):包括定义静态htm名称共三个字段(城市ID(自动编号),城市名称(CityName例如北京),生成htm前缀名(HtmlStartName例如beijing))

3.1个全国酒店列表(Hotel):这里我只建立三个字段(酒店ID(自动编号),城市名称(City),酒店名称(HotelName))方便来引用实例。

4.1个ASP页面(ToHtm.asp)(生成htm使用)

5.1个循环框架页面(IframeToHtm.asp),应用框架批量生成htm

以下给出两个页面的源码

循环框架进行批量生成的页面:IFrameToHtm.asp

<!--#include file="conn.asp"-->'连接数据库
<%
dim rs,sql,j
set rs=Server.createObject("adodb.recordset")
sql="select * from CityHtml"'打开全国城市列表
rs.open sql,conn,1,1
do until rs.eof'循环各大城市%>
<!--以下应用框架打开ToHtml生成页面-->
<IFRame name="LoadRcHtm<%=j%>" frameborder=0 width=100% height=30 scrolling=no src="ToHtml.asp?City=<%=cstr(rs("city"))%>&HtmlStartName=<%=rs("HtmlStart")%>"></IFrame>

<%rs.movenext
loop%>

生成程序页面:ToHtm.asp 我在源码大概写上注释**

<!--#include file="conn.asp"-->'数据连接文件
<%
On Error Resume Next'容错处理
Dim City'定义取得要生成页面的城市
City=Request.Querystring("City")'获取生成的城市酒店值从框架传过来的在后面将介绍
HtmlStartName=Request.Querystring("HtmlStartName")'获得生成htm文件名前缀
Dim sql'搜索字符串,这里我就直接打开表不用搜索变量了,搜索条件按自己写就可以
sql="select * from Hotel where [City] = '" & City & "' "
Dim oRs'数据操作对象
Dim PageCounts'实现分页生成必须得知呀有多少页
Set oRs = Server.createObject("ADODB.Recordset")
oRs.Open Sql,oConn,1,1'找开酒店等于City变量的表
oRs.pagesize=10'十个记录为一页
PageCounts=oRs.pagecount'得出要生成多少个页面,循环生成使用
Dim fs'定义fso文件对象
Dim folders'存放生成静态页的文件夹名称
Dim Filestart'定义生成htm文件前缀
Set fs=Server.createObject("Scripting.FileSystemObject")
Dim i
for i=1 to PageCounts'开始循环生成页面,也就是分页生成了
page=i
oRs.absolutepage=i'页码
rowcount=oRs.pagesize'当页记录数
folders=server.mappath("CityHtml")
if (fs.FolderExists(folders)) then'判断文件夹是否存在
else
fs.createFolder(folders)'不存在则创建CityHtml文件夹
end if
if i=1 then
Filestart=HtmlStartName'如果为第一页则定义文件名为传值名.例如beijing则为beijing.htm
else
Filestart=HtmlStartName&i'如果第二页则为beijing+1例如有两页也就是i等于2则为 beijing2.htm如此类推...(.htm后缀就在后面加上)
end if
Dim files'定义生成文本文件名称变量
Dim filez'定义文件路径名称变量
files=Filestart&".txt"'本文件名称
filez=folders&"\"&"files'文本文件路径
'册除文件
Dim checkfile'检查文本文件是否已经存在,是则删除
checkfile=server.mappath("CityHtml\"&Filestart&".htm")'检查htm文件是否已经存在,是则删除
if (fs.FileExists(checkfile)) then'检查htm文件是否已经存在,是则删除
Dim df'定义文件对象*删除文件使用*
Set df=fs.GetFile(checkfile)'定义要册除的文件
df.delete'册除文件
end if'判断结束
Dim ts'定义写入文件对象
set ts = fs.createtextfile(filez,true) '开启写入文件内容**我在正文只简单写入酒店名称和静态数字分页显示**
ts.write("<Html><Head><Title>生成"&City&"城市酒店</Title>"&vbcrlf)'之后就是要生成的正文件内容了跟使用Response.write
ts.write("<META http-equiv=Content-Type content=text/html; charset=gb2312>"&vbcrlf)
ts.write("<meta name=keywords content="&city&"酒店>"&vbcrlf)
ts.write("<link href='/Style/style.css' rel='stylesheet' type='text/css'></head><body topmargin=0>"&vbcrlf)
ts.Write("<TABLE WIDTH=760 cellspacing=0 cellpadding=0 align=center>"&vbcrlf&_
"<TR><TD width='100%'>"&vbcrlf)
'分页输出开始
'数字分页程序原理在这我就不多说了,不懂的朋友可在网上搜索一下
Dim page'当前页
Dim Page2'数字分页变量
Dim s'数字分页变量
if page=1 then
ts.write (" [首 页] [前一页] ")
else
ts.write (" <a href="&HtmlStartName&".htm"&" class=blue>[首 页]</a> <a href="&HtmlStartName&Replace(page-1,1,"")&".htm"&" class=blue>前一页</a> ")
end if
page2=(page-(page mod 10))/10
if page2<1 then page2=0
for s=page2*10-1 to page2*10+10
if s>0 then
if s=cint(page) then
ts.write (" <font color='#000000'>["& s & "]</font>")
else
if s=1 then
ts.write (" <a href="&HtmlStartName&replace(s,1,"")&".htm"&" class=blue>["& s &"]</a>")
else
ts.write (" <a href="&HtmlStartName&s&".htm"&" class=blue>["& s &"]</a>")
end if
end if
if s=ors.pagecount then
exit for
end if
end if
next
if cint(page)=ors.pagecount then
ts.write (" [后一页] [尾 页]")
else
ts.write (" <a href="&HtmlStartName&page+1&".htm"&" class=blue>[后一页]</a> <a href="&HtmlStartName&ors.pagecount&".htm"&" class=blue>[尾 页]</a>")
end if
ts.write("</TD></TR>")
'分页输出结束
do while not ors.eof and rowcount>0 '输出酒店名称
ts.write("<TR><TD width='100%'>"&oRs.Fields("Chinese_Name")&"</TD></TR>"&vbcrlf)
oRs.movenext
rowcount=rowcount-1'当页记录数-1 loop
ts.write("</Table></body></html>"&vbcrlf)
ts.close
set ts=nothing '释放对象
Dim EditFile'定义改写文件变量
Set EditFile = fs.GetFile(filez)'设置改写文件对象
EditFile.name= left(EditFile.name,len(EditFile.name)-4)&".htm" '改写文本文件成htm
next'循环生成结束(分页生成)
set EditFile=nothing '释放对象
set fs=nothing'释放对象
if err.number<>0 then '处理生成错误
Response.write(City&"更新时发生未知错误<A href=ToHtml.asp?City="&City&"&HtmlName="&HtmlStartName&">重新更新</A>")
else
Response.Write(City&"酒店更新已完成 "&Now())
end if
%>
Comments Feed Comments Feed: http://www.4evil.org/feed.asp?q=comment&id=43

There is no comment on this article.

Post Comment
Smilies
[smile] [confused] [cool] [cry]
[eek] [angry] [wink] [sweat]
[lol] [stun] [razz] [redface]
[rolleyes] [sad] [yes] [no]
[heart] [star] [music] [idea]
Enable UBB Codes
Auto Convert URL
Show Smilies
Hidden Comment
Username:   Password:   Register Now?
Security Code * Please Enter the Security Code