添加到 Google
联互网 PSArtCMS
  当前位置: 首页在线文档网页编程 → 正文
收集了一些asp生成html代码
  • 编辑:admin
  • 来源:互联网
  • 阅读:
  • 时间:2008-2-2 17:11:09

首先这项技术的优点是:(1)减轻数据库的压力;(2)把数据库和页面隔离开来
然后给大家讲一下实现原理..


ASP生成HTML其实是使用服务器的FSO内置对象..
其定义方法为
set fs=createobject("scripting.filesystemobject") ’设置FSO对象


在建立FSO对象后就可以对服务器进行文件及文件夹管理操作...
所以在服务器新建一个网页文件也是很轻松的事...


sub SaveText(FileName,Data)  ’这是一个用于写文本文件的过程
    dim fs,ts,path ’定义变量
set fs=createobject("scripting.filesystemobject") ’设置FSO对象
if instr(filename,":\")<>0 then ’判断是不是绝对路径
   path=filename
else
   path=server.MapPath(FileName)
end if
    set ts=fs.createtextfile(path,true) ’创建文件对象
    ts.writeline(data) ’写数据
    ts.close ’关闭对象
    set ts=nothing
    set fs=nothing
end sub


这是一个建立所在类型文件的子程序..
对重要语句都给了一定的注释...
功能就是向服务器的指定路径创建一个文件并将数据写进去..
其调用方式为:
savetext "D:\chris.html","chris"
大家可以在自己的电脑上试一下..
第一个参数为Chris.html
文件内容为chris


了解了创建文件的原理之后就可以对网站进行批量网页生成..


但是在生成以前我们必须得为生成的页面制作一个模板...


下面我举个例子..


Mode.asp
------------------
<body onLoad="window.focus();">
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0" class="table">
  <tr>
    <td><br>
      <table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td style="LEFT: 0px; WIDTH: 600 xp; WORD-WRAP: break-word"><p><font size="2"><%=rs("bigclass")%> -> <%=rs("smallclass")%> -> <font color="#FF9B9B"><%=rs("title")%></font></font></p>
            <p><font size="2"><%response.Write(ubbcode(rs("content")))%></font></p>
            <p>&nbsp;</p>
            <p align="right"><font size="2">摘自:<%=rs("path")%>  </font></p></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td><div align="center"><font size="2"><br>
        发布时间:<font color="#FF9B9B"><%=rs("time")%></font>  浏览次数:<font color="#FF9B9B"><%=rs("browse")%></font></font></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
</body>

这是一个模板文件....


下面要做的操作就是制作一个生成页面...


这个生成页面要做的工作是将Mode.asp的文件内容读出.然后将其需要动态替换的地方替换成需要的内容..


set fso=server.CreateObject("scripting.filesystemobject") ’创建一个FSO对象
set myfile=fso.getfile(filepath)    ’设置一个文件对象..filepath就是这个模板文件的名称
set ts=myfile.openastextstream     ’设置一个文本对象..并打开这个对象...
if not ts.atendofstream then content=changecontent(ts.readline)
do while not ts.atendofstream
content=content+vbcrlf
content=content+changecontent(ts.readline)
loop
将模板文件的内容赋值到content变量里...
再对其模板内容处理后生成新的网页文件...
例如
Replace(content,"<%=rs("title")%"&">",title)
将模板文件的<%=Rs("title")%>替换为文章标题...
其他的Content,Time,Browse以此类推...
Content进行处理后就是一个完整的网页文件..
将其输出就完成了网页的生成..
sub SaveText(FileName,Data)  ’这是一个用于写文本文件的过程
    dim fs,ts,path ’定义变量
set fs=createobject("scripting.filesystemobject") ’设置FSO对象
if instr(filename,":\")<>0 then ’判断是不是绝对路径
   path=filename
else
   path=server.MapPath(FileName)
end if
    set ts=fs.createtextfile(path,true) ’创建文件对象
    ts.writeline(data) ’写数据
    ts.close ’关闭对象
    set ts=nothing
    set fs=nothing
end sub
savetext CreateFileName,Content ’调用写文件子程序
使用模板自动生成原理基本上就是这样...
主要问题在于大家使用过程中的一些细节问题的注意...
一会儿我会让大家看一个比较成熟的FSO的展示程序...
set myfile=fso.getfile(uta(filepath))
----------------------
在FSO对象的基础上建立一个文件对象...
myfile=你指定的那个文件..
if not ts.atendofstream then content=changecontent(ts.readline) ’判断文件是否为空.如果不为空才进行赋值操作
do while not ts.atendofstream ’AtEndOfStream判断是不是文件的末尾
content=content+vbcrlf
content=content+changecontent(ts.readline)
loop
FSO对象的属性和方法比较多...如果大家感兴趣可以参考一下VBS参考手册..
我在这里只是起一个引导作用...给大家讲解一下成生网页文件..

content=content+vbcrlf
content=content+changecontent(ts.readline)
是做什么的
------------------------------------
把文件按行读出来...
AtEndOfStream判断是不是文件的末尾 刚才Chris已经讲了
do while not ts.atendofstream  ’AtEndOfStream判断是不是文件的末尾 content=content+vbcrlf
content=content+changecontent(ts.readline)
loop
的意思就是说,把文件中的一行一行读出来
只是一个函数..
function uta(val)
uta=replace(val,"*","&")
end function


filepath是在哪里取的?


filepath就是你那个文件所在地呀

changecontent这个函数有什么用? 

changecontent是一个函数,可能是替换里面的一些字符
那些都是一些Replace的替换操作...
你们根据自己需要进行处理...
不一定必须...

整体原理就是利用FSO的创建文件方法在服务器创建一个网页文件..
然后把代码写到创建的文件里...就是这样..

方法一:FSO

Set fs = CreateObject("Scripting.FileSystemObject")
NewFile=Server.MapPath("/asp/chap06/at/newfile.html")
’新建一文件/newfile.html,若该文件已存在,则覆盖它
Set a = fs.CreateTextFile(NewFile, True)
Response.Write"新文件已建立!"
a.close
File=Server.MapPath("newfile.html")
Set txt=fs.OpenTextFile(File,8,True)               ’打开成可以在结尾写入数据的文件
data1="这句话是使用WriteLine方法写入的哦!~~"
txt.WriteLine data1
data2="这句话是使用Write方法写入的哦!~~"
txt.Write data2
txt.Close

方法二:XMLHTTP

<%
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
 ’把下面的地址替换成你的首页的文件地址,一定要用http://开头的绝对路径,不能写相对路径
 xml.Open "GET", "http://www.phpup.com", 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("aa.htm"), True)
MyFile.WriteLine(BodyText)
MyFile.Close

其他:

1

下面的例子是将、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
%>

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

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
public tempelatefile,tmpdata
sub ofile()’打开文件,并把文件内容放到tmpdata
 on error resume next
 tmpdata=""
 set Astream=Server.CreateObject ("Adodb.Stream")
 Astream.type=2’文件类型文本
 Astream.Mode = 3’读写
 Astream.open
 Astream.CharSet = "GB2312"’字符集
 Astream.LoadFromFile(tempelatefile)’从文件装载
 Assp=Astream.size
 if err.number<>0 then
  xz=-18
  response.Write tempelatefile&"<br>"
  err.clear
  tmpdata=""
 else
  tmpdata=Astream.ReadText(Assp)
 end if
 
end sub

sub save_file()
 ofile()
 recfilen=server.MapPath(dts)
 Astream.Flush
 Astream.close
 Astream.type=2
 Astream.Mode = 3
 Astream.open
 Astream.CharSet = "GB2312"
 Astream.position=0
 Astream.Writetext tmpdata,1’写入数据到stream
 Astream.SaveToFile recfilen,2’保存到文件
end sub

function dts()’产生随机文件名
 if len(month(now()))>1 then
  mm=month(now())
 else
  mm="0"&month(now())
 end if
 if len(day(now()))>1 then
  d=day(now())
 else
  d="0"&day(now())
 end if
 if len(hour(now()))>1 then
  h=hour(now())
 else
  h="0"&hour(now())
 end if
 if len(minute(now()))>1 then
  m=minute(now())
 else
  m="0"&minute(now())
 end if
 if len(second(now()))>1 then
  s=second(now())
 else
  s="0"&second(now())
 end if
 Randomize
 upperbound=9999
 lowerbound=1000
 rds=Int((upperbound - lowerbound + 1) * Rnd + lowerbound) 
 dts="htm/"&year(now())&mm&d&h&m&s&rds&".htm"
end function
title=request.Form("title")
content=request.Form("content")
tmpdata=replace(tmpdata,"<title></title>",title)’以拥护提交内容替换
tmpdata=replace(tmpdata,"<content></content>",content)
tempelatefile=server.MapPath("tempelate/1.htm")’模版文件
save_file()
%>

一个用ASP生成html的新方法

   目前已经有很多生成html的新闻系统,但是都是用的模板,本函数实现把asp页面产生的html代码保存成为一个html文件,这样就没有必要改动原来的页面就可以轻松完成一个生成html的新闻系统了。^_^

由于代码比较短,这里就不进行注释了

<%
’当目标页面的包含文件即#include的页面里边存在response.End()的时候本程序有问题
’注意:本文件一定要放在filename指向的文件的同一目录下
dim hughchiu_rtcode
Function get_exe_code(filename)
 dim execode
 dim tmp_str
 Dim re,re1,content,fso,f,aspStart,aspEnd
 dim ms,m
 execode = ""
 set fso=CreateObject("Scripting.FileSystemObject")
 set f=fso.OpenTextFile(server.mappath(filename))
 content=f.ReadAll
 f.close
 set f=nothing
 set fso=nothing
 
 set re = new regexp
 re.ignorecase = true
 re.global = true
 re.pattern = "\<\%\@[^\%]+\%\>"
 content = re.replace(content,"")
 
 re.global = false
 re.pattern = "\<\!\-\-\s*\#include\s*file\s*=\s*\""([^\""]+)\""\s*\-\-\>"
 do
 set ms = re.execute(content)
if ms.count<>0 then
set m = ms(0)
tmp_str = get_exe_code(m.submatches(0))
content = re.replace(content, tmp_str)
else
exit do
end if
 loop
 set m = nothing
 set ms = nothing
 
 re.pattern="^\s*="
 aspEnd=1
 aspStart=inStr(aspEnd,content,"<%")+2
 
 set re1=new RegExp
 re1.ignorecase = true
 re1.global = false
 re1.pattern = "response\.Write(.+)"
 
 do while aspStart>aspEnd+1
  execode = execode&vbcrlf&" hughchiu_rtcode = hughchiu_rtcode&"""&replace( replace(Mid(content,aspEnd,aspStart-aspEnd-2),"""",""""""), vbcrlf, """&vbcrlf&""")&""""&vbcrlf
  aspEnd=inStr(aspStart,content,"%\>")+2
  tmp_str = Mid(content,aspStart,aspEnd-aspStart-2)
 
do
set ms = re1.execute(tmp_str)
if ms.count<>0 then
set m = ms(0)
tmp_str = re1.replace(tmp_str, " hughchiu_rtcode = hughchiu_rtcode&"&m.submatches(0))
else
exit do
end if
loop
 
  set m = nothing
  set ms = nothing
 
  execode = execode& re.replace(tmp_str,"hughchiu_rtcode = hughchiu_rtcode&")

  aspStart=inStr(aspEnd,content,"<%")+2
 loop
 
 set re1 = nothing
 set re=nothing
 
 execode = execode&vbcrlf&" hughchiu_rtcode = hughchiu_rtcode&"""&replace( replace(Mid(content,aspEnd), """", """"""), vbcrlf, """&vbcrlf&""" )&""""&vbcrlf
 get_exe_code = "<%"&execode&"%\>"
End Function

 

function asp2html(filename)
dim code
code = replace( replace( replace( get_exe_code(filename), "hughchiu_rtcode = hughchiu_rtcode&"""""&vbcrlf, "" ), "<%", "" ), "%\>", "" )
’response.Write(code)
execute(code)
’response.Write( hughchiu_rtcode )
asp2html = hughchiu_rtcode
end function
%>


使用范例:
set fso=CreateObject("Scripting.FileSystemObject")
set f=fso.CreateTextFile( server.mappath( "youpage.htm" ), true )
f.WriteLine( asp2html("youpage.asp") )
f.close
set f = nothing
set fso = nothing

希望这个函数对大家有点用,由于水平有限,有错的地方请大家指出,并希望能加以改进。

 

asp生成html
<!--index.htm---------------------->
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>

<body>
<table width="770" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><form name="form1" method="post" action="send.asp">
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ccffff">
<tr>
<td height="20"><div align="center">发送消息</div></td>
</tr>
<tr>
<td><div align="center">
<textarea name="msg" cols="100" rows="6"></textarea>
</div></td>
</tr>
<tr>
<td><div align="center">
<input type="submit" name="submit" value="submit">
<input type="reset" name="submit2" value="reset">
</div></td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>

"//send.asp
<%
function chan_time(shijian)"转换日期时间函数
s_year=year(shijian)
if len(s_year)=2 then s_year="20"&s_year
s_month=month(shijian)
if s_month<10 then s_month="0"&s_month
s_day=day(shijian)
if s_day<10 then s_day="0"&s_day
s_hour=hour(shijian)
if s_hour<10 then s_hour="0"&s_hour
s_minute=minute(shijian)
if s_minute<10 then s_minute="0"&s_minute
chan_time=s_year & s_month & s_day & s_hour & s_minute
end function

function chan_data(shijian) "转换日期时间函数
s_year=year(shijian)
if len(s_year)=2 then s_year="20"&s_year
s_month=month(shijian)
if s_month<10 then s_month="0"&s_month
s_day=day(shijian)
if s_day<10 then s_day="0"&s_day
chan_data=s_year & s_month & s_day
end function
function chan_file(shijian)"转换日期时间函数
s_month=month(shijian)
if s_month<10 then s_month="0"&s_month
s_day=day(shijian)
if s_day<10 then s_day="0"&s_day
s_hour=hour(shijian)
if s_hour<10 then s_hour="0"&s_hour
s_minute=minute(shijian)
if s_minute<10 then s_minute="0"&s_minute
s_ss=second(shijian)
if s_ss<10 then s_ss="0"&s_ss
chan_file = s_month & s_day & s_hour & s_minute & s_ss
end function
top="<html><head><title>news</title></head><body>"
botom="</body></html>"
msg=request.form("msg")
msg=replace(msg,vbcrlf,"")
msg=replace(msg,chr(9),"")
msg=replace(msg," ","&nbsp;")
msg=replace(msg,"/r/n","<br>")
msg=replace(msg,"/n","<br>")
msg=top&msg&botom
set fs=server.createobject("scripting.filesystemobject")
all_tree2=server.mappath("news")&"/"&chan_data(now)
if (fs.folderexists(all_tree2)) then"判断今天的文件夹是否存在
else
fs.createfolder(all_tree2)
end if
pass=chan_file(now)
randomize "使用系统计时器来初始化乱数产生器
pass=rnd(pass)
pass=get_pass(pass)
pass=left(pass,10)
file1=pass
files=file1&".txt"
filez=all_tree2&"/"&files

set ts = fs.createtextfile(filez,true) "写文件
for z=1 to len(msg)
write_now=mid(msg,z,1)
ts.write(write_now)
next
" ts.writeline(all_msg)
ts.close
set ts=nothing "文件生成

if err.number<>0 or err then%>
<script language="javascript">
alert("不能完成")
</script>
<%else%>
<script language="javascript">
alert("已完成")
history.back();
</script>
<%end if
set myfile = fs.getfile(filez)
all_tree2=server.mappath("news")&"/"&chan_data(now)
if (fs.folderexists(all_tree2)) then
else
fs.createfolder(all_tree2)
end if
myfile.name= left(myfile.name,len(myfile.name)-4)&".htm"
set myfile=nothing
set fs=nothing
set fdir=nothing
function get_pass(pass)

pass=cstr(pass)
pass=replace(pass," ","")
pass=replace(pass," ","")
pass=replace(pass,"-","")
pass=replace(pass," ","")
pass=replace(pass,":","")
pass=replace(pass,".","")
pass=replace(pass,"+","")
pass=replace(pass,"_","")
pass=replace(pass,"<","")
pass=replace(pass,">","")
pass=replace(pass,"!","")
pass=replace(pass,"@","")
pass=replace(pass,"#","")
pass=replace(pass,"$","")
pass=replace(pass,"%","")
pass=replace(pass,"^","")
pass=replace(pass,"&","")
pass=replace(pass,"*","")
pass=replace(pass,"(","")
pass=replace(pass,")","")
pass=replace(pass,"=","")
pass=replace(pass,"/","")
pass=replace(pass,"/","")
pass=replace(pass,"|","")
get_pass=pass

end function
%>