家园首页 Php4手册 我的相册 家园下载 个人文集 给我留言
我形我塑,网络相册,www.5x54.com 相册 相册 心情相册,少女相册 免费申请,我形我塑免费相册
 家园 个人文集   http://www.5x54.com
 


ASP生成Html文件静态页面


作者:网上收集    点击: 读取中...

网页生成静态Html文件有许多好处,比如生成html网页有利于被搜索引擎收录,不仅被收录的快还收录的全.前台脱离了数据访问,减轻对数据库访问的压力,加快网页打开速度. 所以吟清最近对生成html比较感兴趣,看了不少文章,也有一点点收获.  (%$$#^33067*&%2)我型我塑www.5x54.com
(%$$#^33067*&%2)我型我塑www.5x54.com
1,下面这个例子直接利用FSO把html代码写入到文件中然后生成.html格式的文件  (%$$#^33067*&%2)我型我塑www.5x54.com
引用代码:<%  (%$$#^33067*&%2)我型我塑www.5x54.com
filename="test.htm"  (%$$#^33067*&%2)我型我塑www.5x54.com
if request("body")<>"" then  (%$$#^33067*&%2)我型我塑www.5x54.com
set fso = Server.CreateObject("Scripting.FileSystemObject")  (%$$#^33067*&%2)我型我塑www.5x54.com
set htmlwrite = fso.CreateTextFile(server.mappath(""&filename&""))  (%$$#^33067*&%2)我型我塑www.5x54.com
htmlwrite.write "<html><head><title>" & request.form("title") & "</title></head>"  (%$$#^33067*&%2)我型我塑www.5x54.com
htmlwrite.write " <body>输出Title内容: " & request.form("title") & "<br /> 输出 Body内容:" & request.form("body")& "</body></html>"  (%$$#^33067*&%2)我型我塑www.5x54.com
htmlwrite.close  (%$$#^33067*&%2)我型我塑www.5x54.com
set fout=nothing  (%$$#^33067*&%2)我型我塑www.5x54.com
set fso=nothing  (%$$#^33067*&%2)我型我塑www.5x54.com
end if  (%$$#^33067*&%2)我型我塑www.5x54.com
%>  (%$$#^33067*&%2)我型我塑www.5x54.com
<form name="form" method="post" action="">  (%$$#^33067*&%2)我型我塑www.5x54.com
<input name="title" value="Title" size=26>  (%$$#^33067*&%2)我型我塑www.5x54.com
<br>  (%$$#^33067*&%2)我型我塑www.5x54.com
<textarea name="body">Body</textarea>  (%$$#^33067*&%2)我型我塑www.5x54.com
<br>  (%$$#^33067*&%2)我型我塑www.5x54.com
<br>  (%$$#^33067*&%2)我型我塑www.5x54.com
<input type="submit" name="Submit" value="生成html">  (%$$#^33067*&%2)我型我塑www.5x54.com
</form> (%$$#^33067*&%2)我型我塑www.5x54.com
(%$$#^33067*&%2)我型我塑www.5x54.com
2、但是按照上面的方法生成html文件非常不方便,第二种方法就是利用模板技术,将模板中特殊代码的值替换为从表单或是数据库字段中接受过来的值,完成模板功能;将最终替换过的所有模板代码生成HTML文件.这种技术采用得比较多,大部分的CMS都是使用这类方法.  (%$$#^33067*&%2)我型我塑www.5x54.com
template.htm ' //模板文件  (%$$#^33067*&%2)我型我塑www.5x54.com
引用代码:<html>  (%$$#^33067*&%2)我型我塑www.5x54.com
<head>  (%$$#^33067*&%2)我型我塑www.5x54.com
<title>$title$ by aspid.cn</title>  (%$$#^33067*&%2)我型我塑www.5x54.com
</head>  (%$$#^33067*&%2)我型我塑www.5x54.com
<body>  (%$$#^33067*&%2)我型我塑www.5x54.com
$body$  (%$$#^33067*&%2)我型我塑www.5x54.com
</body>  (%$$#^33067*&%2)我型我塑www.5x54.com
</html> ?  (%$$#^33067*&%2)我型我塑www.5x54.com
TestTemplate.asp '// 生成Html <%  (%$$#^33067*&%2)我型我塑www.5x54.com
Dim fso,htmlwrite  (%$$#^33067*&%2)我型我塑www.5x54.com
Dim strTitle,strContent,strOut  (%$$#^33067*&%2)我型我塑www.5x54.com
'// 创建文件系统对象  (%$$#^33067*&%2)我型我塑www.5x54.com
Set fso=Server.CreateObject("Scripting.FileSystemObject")  (%$$#^33067*&%2)我型我塑www.5x54.com
'// 打开网页模板文件(%$$#^33067*&%2)我型我塑www.5x54.com,读取模板内容  (%$$#^33067*&%2)我型我塑www.5x54.com
Set htmlwrite=fso.OpenTextFile(Server.MapPath("Template.htm"))  (%$$#^33067*&%2)我型我塑www.5x54.com
strOut=f.ReadAll  (%$$#^33067*&%2)我型我塑www.5x54.com
htmlwrite.close  (%$$#^33067*&%2)我型我塑www.5x54.com
strTitle="生成的网页标题"  (%$$#^33067*&%2)我型我塑www.5x54.com
strContent="生成的网页内容"  (%$$#^33067*&%2)我型我塑www.5x54.com
'// 用真实内容替换模板中的标记  (%$$#^33067*&%2)我型我塑www.5x54.com
strOut=Replace(strOut,"$title$",strTitle)  (%$$#^33067*&%2)我型我塑www.5x54.com
strOut=Replace(strOut,"$body$",strContent)  (%$$#^33067*&%2)我型我塑www.5x54.com
'// 创建要生成的静态页  (%$$#^33067*&%2)我型我塑www.5x54.com
Set htmlwrite=fso.CreateTextFile(Server.MapPath("test.htm"),true)  (%$$#^33067*&%2)我型我塑www.5x54.com
'// 写入网页内容  (%$$#^33067*&%2)我型我塑www.5x54.com
htmlwrite.WriteLine strOut  (%$$#^33067*&%2)我型我塑www.5x54.com
htmlwrite.close  (%$$#^33067*&%2)我型我塑www.5x54.com
Response.Write "生成静态页成功!"  (%$$#^33067*&%2)我型我塑www.5x54.com
'// 释放文件系统对象  (%$$#^33067*&%2)我型我塑www.5x54.com
set htmlwrite=Nothing  (%$$#^33067*&%2)我型我塑www.5x54.com
set fso=Nothing  (%$$#^33067*&%2)我型我塑www.5x54.com
%>  (%$$#^33067*&%2)我型我塑www.5x54.com
(%$$#^33067*&%2)我型我塑www.5x54.com
3、第三种方法就是用XMLHTTP获取动态页生成的HTML内容(%$$#^33067*&%2)我型我塑www.5x54.com,再用ADODB.Stream或者 Scripting.FileSystemObject保存成html文件。这句话是在蓝色理想上看到的,对XMLHTTP吟清还不熟悉正在找资料了解. 找到一段XMLHTTP生成Html的代码参考一下.  (%$$#^33067*&%2)我型我塑www.5x54.com
引用代码: (%$$#^33067*&%2)我型我塑www.5x54.com
<%  (%$$#^33067*&%2)我型我塑www.5x54.com
'常用函数  (%$$#^33067*&%2)我型我塑www.5x54.com
'1、输入url目标网页地址(%$$#^33067*&%2)我型我塑www.5x54.com,返回值getHTTPPage是目标网页的html代码  (%$$#^33067*&%2)我型我塑www.5x54.com
function getHTTPPage(url)  (%$$#^33067*&%2)我型我塑www.5x54.com
dim Http  (%$$#^33067*&%2)我型我塑www.5x54.com
set Http=server.createobject("MSXML2.XMLHTTP")  (%$$#^33067*&%2)我型我塑www.5x54.com
Http.open "GET",url,false  (%$$#^33067*&%2)我型我塑www.5x54.com
Http.send()  (%$$#^33067*&%2)我型我塑www.5x54.com
if Http.readystate<>4 then  (%$$#^33067*&%2)我型我塑www.5x54.com
exit function  (%$$#^33067*&%2)我型我塑www.5x54.com
end if  (%$$#^33067*&%2)我型我塑www.5x54.com
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")  (%$$#^33067*&%2)我型我塑www.5x54.com
set http=nothing  (%$$#^33067*&%2)我型我塑www.5x54.com
if err.number<>0 then err.Clear  (%$$#^33067*&%2)我型我塑www.5x54.com
end function  (%$$#^33067*&%2)我型我塑www.5x54.com
(%$$#^33067*&%2)我型我塑www.5x54.com
2、转换乱玛(%$$#^33067*&%2)我型我塑www.5x54.com,直接用xmlhttp调用有中文字符的网页得到的将是乱玛(%$$#^33067*&%2)我型我塑www.5x54.com,可以通过adodb.stream组件进行转换  (%$$#^33067*&%2)我型我塑www.5x54.com
引用代码: (%$$#^33067*&%2)我型我塑www.5x54.com
Function BytesToBstr(body,Cset)  (%$$#^33067*&%2)我型我塑www.5x54.com
dim objstream  (%$$#^33067*&%2)我型我塑www.5x54.com
set objstream = Server.CreateObject("adodb.stream")  (%$$#^33067*&%2)我型我塑www.5x54.com
objstream.Type = 1  (%$$#^33067*&%2)我型我塑www.5x54.com
objstream.Mode =3  (%$$#^33067*&%2)我型我塑www.5x54.com
objstream.Open  (%$$#^33067*&%2)我型我塑www.5x54.com
objstream.Write body  (%$$#^33067*&%2)我型我塑www.5x54.com
objstream.Position = 0  (%$$#^33067*&%2)我型我塑www.5x54.com
objstream.Type = 2  (%$$#^33067*&%2)我型我塑www.5x54.com
objstream.Charset = Cset  (%$$#^33067*&%2)我型我塑www.5x54.com
BytesToBstr = objstream.ReadText  (%$$#^33067*&%2)我型我塑www.5x54.com
objstream.Close  (%$$#^33067*&%2)我型我塑www.5x54.com
set objstream = nothing   (%$$#^33067*&%2)我型我塑www.5x54.com
  (%$$#^33067*&%2)我型我塑www.5x54.com
End Function  (%$$#^33067*&%2)我型我塑www.5x54.com
(%$$#^33067*&%2)我型我塑www.5x54.com
txtURL=server.MapPath("../index.asp")  (%$$#^33067*&%2)我型我塑www.5x54.com
sText = getHTTPPage(txtURL)  (%$$#^33067*&%2)我型我塑www.5x54.com
Set FileObject=Server.CreateObject("Scripting.FileSystemObject")  (%$$#^33067*&%2)我型我塑www.5x54.com
filename="../index.htm"  (%$$#^33067*&%2)我型我塑www.5x54.com
Set openFile=FileObject.OpenTextfile(server.mapPath(filename),2,true) 'true为不存在自行建立  (%$$#^33067*&%2)我型我塑www.5x54.com
openFile.writeline(sText)  (%$$#^33067*&%2)我型我塑www.5x54.com
Set OpenFile=nothing  (%$$#^33067*&%2)我型我塑www.5x54.com
%>  (%$$#^33067*&%2)我型我塑www.5x54.com
<script>  (%$$#^33067*&%2)我型我塑www.5x54.com
alert("静态网页生成完毕");  (%$$#^33067*&%2)我型我塑www.5x54.com
history.back();  (%$$#^33067*&%2)我型我塑www.5x54.com
</script>  (%$$#^33067*&%2)我型我塑www.5x54.com

收藏此文章     打印  更新     编辑此文章    『关闭窗口』