ASP生成静态页面
成功志
ASP生成静态页面
2012-8-15 ok12
这里这个生成静态页面的函数可通用

数据库结构:

id(自动编号)

标题 title(文本)

内容 content(备注)

添加时间 addtime(时间) 这里用来做静态页面的文件名



'连接数据库文件

conn.asp

< %

DB="database/database.mdb"

path=Server.MapPath(DB)

set conn=server.createobject("adodb.Connection")

connstr="provider=Microsoft.Jet.OLEDB.4.0;Data Source="&path

conn.Open connstr

Set rs=server.CreateObject("adodb.recordset")

% >


'动态页面

news.asp


< !-- #include file="conn.asp" -- >

< %

    id=request("id")

 sql="select * from news where id="&id

 rs.open sql,conn,1,1

 If rs.eof Then

 Else

     title=rs("title")

  content=rs("content")

 End If

 rs.close

% >

<style>

body td {font-size:12px;}

</style>

  <TITLE> < %=title% > </TITLE>

     <TABLE border="0" cellpadding="0" cellspacing="0" width="500">

    <TR>

  <TD style="border-bottom:1px #cccccc solid; height:40px; font-weight:bold; text-align:center;">< %=title% ></TD>

    </TR>

    <TR>

  <TD align="center"><div style="margin-top:20px; width:95%">< %=content% ></div></TD>

    </TR>

 </TABLE>



'生成静态页面代码

createhtml.asp


< !-- #include file="conn.asp" -- >

< %

  sql="select * from news"

  rs.open sql,conn,1,1

  If rs.eof Then

  Else  

    path5="./html/" '这个为生成后文件路径

    Do While Not rs.eof

      filename=Replace(Replace(Replace(rs("addtime"),":",""),"-","")," ","")&".html" '这个为生成后的文件名

   url="http://localhost/netfriend/news.asp?id="&rs("id")   '这个为动态页面地址,必须是http://****.asp格式的

   Call createhtml(url,filename,path5)

 rs.movenext

 loop

  End If

  rs.close

  Set rs=Nothing

  Set conn=nothing



  '功能:读取数据库内容,生成静态页面

  '来自:www.aspprogram.cn

  '作者:wangsdong

  '参数说明:

  ' url是动态页面的地址,必须是http://*****/**.asp格式

  ' filename为生成后的文件名

  ' path为存储生成后文件的文件夹名

  '备注:支持原创程序,请保留此信息,谢谢


Function createhtml(url,filename,path)

Set MyFileObject=Server.CreateObject("Scripting.FileSystemObject")

path1=server.mappath(path)&"\"&filename

Set MyTextFile=MyFileObject.CreateTextFile(path1)

strurl=url  

strTmp = GetHTTPPage(trim(strurl))

MyTextFile.WriteLine(strTmp)

MytextFile.Close

response.write "生成"&filename&"成功<br>"

Set MyFileObject=nothing

End function


Function getHTTPPage(url)

 On Error Resume Next

 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

  Response.Write "<p align='center'><font color='red'><b>服务器获取文件内容出错</b></font></p>"

  Err.Clear

 End If 

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

% >

已经搞定,现在就可以运行这个createhtml.asp文件了,如果不成功,注意一下动态页面的地址,根据IIS进行更换

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容