您的位置:首页 > 编程语言 > ASP

asp如何实现静态面页的生成.

2007-04-17 20:17 295 查看
<%
'=============================文件生成相关配置 开始====================================
dim sitepath,homepath,newspath,newslist
sitepath = Server.MapPath("/")
homepath = sitepath&"/html/home/"
newspath = sitepath&"/html/news/"
newslistpath = sitepath&"/html/newslist/"
'=============================文件生成相关配置 结束====================================
function makefile(filetype)
sitepath = Server.MapPath("/")
select case filetype
case "1"
'首页文件
template = sitepath&"/html/temp/home.shtml"
filename = sitepath&"/html/home/"
case "2"
'新闻列表及分页
template = sitepath&"/html/temp/newslist.shtml"
filename = sitepath&"/html/newslist/list"&classid&"_"&pageNo&".shtml"
case "3"
'新闻显示页面
template = sitepath&"/html/temp/news.shtml"
end select
Set fso = Server.CreateObject("Scripting.FileSystemObject")
'读取模板的内容
Set ftemp=fso.OpenTextFile(template, 1)
pencat = ftemp.ReadAll
ftemp.Close
'response.write(bigclassID)
'response.end
'替换掉里面的内容
if pagetitle="" or isnull(pagetitle) then pagetitle = title&"---中国电子信息教育网" end if
pencat=replace(pencat,"@pagetitle",pagetitle)
pencat=replace(pencat,"@title",title)
pencat=replace(pencat,"@Content",Content)
pencat=replace(pencat,"@addtime",addtime)
pencat=replace(pencat,"@Source",StrSource)
pencat=replace(pencat,"@Author",Author)
pencat=replace(pencat,"@keywordstr",Remark)

pencat = replace(pencat,"@bclass",bigclassname)
pencat = replace(pencat,"@bigclassID",bigclassID)
pencat = replace(pencat,"@SmallClassID",SmallClassID)
pencat = replace(pencat,"@add_bclass","<a href=""/html/Column/Column_"&bigclassID&".shtml"">"&bigclassname&"</a>")
pencat = replace(pencat,"@add_sclass","<a href=""/html/newslist/list"&SmallClassID&"_1.shtml"">"&smallclassname&"</a>")

newspath=""
newspath=sitepath&"/html/news/"&year(now())&"/"

If NOT fso.folderexists(newspath) then
fso.createfolder(newspath)
newspath=sitepath&"/html/news/"&year(now())&"/"&month(now())&"/"
fso.createfolder(newspath)
filename=newspath&"/newsview"&makefilename(addtime)&".shtml"
else
newspath=sitepath&"/html/news/"&year(now())&"/"&month(now())&"/"
If NOT fso.folderexists(newspath) then
fso.createfolder(newspath)
filename=newspath&"/newsview"&makefilename(addtime)&".shtml"
else
filename=newspath&"/newsview"&makefilename(addtime)&".shtml"
end if
end if

Set fout = fso.CreateTextFile(filename)
fout.WriteLine pencat
fout.close
Set fso = nothing
'--------------------查检文件生成过程中是否产生错误-------------------
if Err.Number>0 then
makefile = "<div><b>生成不成功:</b></div><div>文件:<font color=""#0000FF"">"&filename&"</font></div><div>产生错误的原因可能为:"&CStr(Err.Description)&"</div><div>错误代码:"&CStr(Err.Number)& "</div>"
else
makefile = "<div><b>生成成功:</b></div><div>文件:<font color=""#0000FF"">"&filename&"</font></div>"
end if
Err.clear()
end function

function makefilename(datestr)
dim getfilename,str
str = Cdate(datestr)
getfilename = year(str)
getfilename = getfilename&right("00"&month(str),2)
getfilename = getfilename&right("00"&day(str),2)
getfilename = getfilename&right("00"&hour( str),2)
getfilename = getfilename&right("00"&minute(str),2)
getfilename = getfilename&right("00"&Second(str),2)
makefilename = Cstr(getfilename)
end function
Function SaveFile(fileBody,filePath)
'fileBody ---- 要写入文件的内容(字符串)
'filePath ---- 要生成文件的路径(绝对地址加文件名)
'-------------------------本函数为将流内容存盘的函数------------------
On Error Resume Next
Set objStream = Server.CreateObject("ADODB.Stream")
'--------------建立ADODB.Stream对象,必须要ADO 2.5以上版本------------
objStream.Type = 2
objStream.CharSet="gb2312"
'-------------以文本模式打开------------------------------------------
objStream.Open
objstream.WriteText fileBody,1
'--------------------将字符串内容写入文件-----------------------------
'路径注意
objstream.SaveToFile filePath,2
'-----------------------关闭对象,释放资源----------------------------
objstream.Close()
set objstream = nothing
'--------------------查检文件生成过程中是否产生错误-------------------
if Err.Number>0 then
SaveFile = "<div><b>生成不成功:</b></div><div>文件:<font color=""#0000FF"">"&filePath&"</font></div><div>产生错误的原因可能为:"&CStr(Err.Description)&"</div><div>错误代码:"&CStr(Err.Number)& "</div>"
else
SaveFile = "<div><b>生成成功:</b></div><div>文件:<font color=""#0000FF"">"&filePath&"</font></div>"
end if
Err.clear()
End Function
%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: