asp自动生成html文件的方法
生活随笔
收集整理的這篇文章主要介紹了
asp自动生成html文件的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1,設計數據庫testmb.mdb
新建表moban:字段m_id(自動編號,主關鍵字);字段m_html(備注類型)
2,假設第一模板內容代碼
將下列代碼拷貝到m_html字段中
以下是代碼片段:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>testmb</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="2">
<tr align="right" bgcolor="#CCCCCC">
<td height="20" colspan="2">$ cntop$ </td>
</tr>
<tr valign="top">
<td width="25%" bgcolor="#e5e5e5">$ cnleft$ </td>
<td width="74%" bgcolor="#f3f3f3">$ cnright$ </td>
</tr>
</table>
</body>
</html>
注意$ cntop$ 、$ cnleft$ 、$ cnright$ ,它們將要實現某些具體的程序功能
3,建立數據庫連接文件conn.asp
以下是代碼片段:
<%
set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("testmb.mdb")
conn.Open connstr
%>
4,建立特殊字符串轉換所需要的庫文件lib.asp
該文件的主要作用是將實現某些功能的ASP程序做成字程序,以方便調用。
以下是代碼片段:
<%
dim topcode
sub cntop()
topcode="現在時間是:"
topcode=topcode&now()
end sub
dim leftcode,i
sub cnleft()
for i = 1 to 5
leftcode=leftcode&"<p>cnbruce.com"
next
end sub
dim rightcode
sub cnright()
for i = 1 to 9
rightcode=rightcode&"<hr color="&i&i&i&i&i&i&">"
next
end sub
%>
5,最后,調用數據庫中的模板代碼,將特殊字符串轉換。
以下是代碼片段:
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%
sql="select * from moban where m_id=1"
set rs=Server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
mb_code=rs("m_html")
rs.close
set rs=nothing
cntop()
mb_code=replace(mb_code,"$ cntop$ ",topcode)
cnleft()
mb_code=replace(mb_code,"$ cnleft$ ",leftcode)
cnright()
mb_code=replace(mb_code,"$ cnright$ ",rightcode)
response.write mb_code
%>
該頁主要作用是將模板代碼進行顯示,并將其中的特殊代碼轉變為相對應子程序功能。
至此,ASP的模板功能基本完成,剩下的就是:建立具備編輯模板功能的程序頁面,將庫文件改變為自己所需要程序功能……
2,2HTML技術又該如何實現呢?如何使得ASP頁面轉變為HTML?一般都會想到FSO組件,因為該組件能新建任何文件格式。
那么其整個運行過程是怎么樣的呢?
a,提供信息輸入頁面進行信息收集;
b,接受信息值先保存數據庫,再FSO生成文件;
c,技術性完成任務,顯示剛被創建的HTML文件的路徑地址。
該技術的實現過程中有如下幾個難點:
i,FSO生成的文件是直接放在一個大文件夾下,還是單獨放在某個每日更新的子文件夾中?可能表述不準確,這樣理解吧:相信通過FSO生成的文件隨著時間的推移,文件會越來越多,管理也會越來越亂……通常你可能看到一些地址諸如 www.xxx.com/a/2004-5-20/200405201111.html ; 可以分析得出應該是建立了當前日期的文件夾。這樣,一天就是一個文件夾的頁面內容,查看管理也就顯得比較合理。
ii,我在試圖通過以上方法建立文件夾的時候,又發現了第二個問題。第一次通過FSO建立以當前日期命名的文件夾,沒有問題。當我有新的文件需要生成時,因為是同一個程序,所以,其又將會執行建立同樣的文件夾。此時,FSO組件會發現該路徑已存在……卡殼-_-! 繼續處理,在首行添加代碼:
On Error Resume Next
嘿嘿,達到自欺欺人、掩耳盜鈴的效果。
iii,文件夾是建立了,文件該如何建立呢?主要也就是文件名的生成。當然這個就需要自己來寫個函數,功能就是如何生成文件名:)
<%
function makefilename(fname)
fname = fname ''前fname為變量,后fname為函數參數引用
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename = fname & ".html"
end function
%>
引用函數則:
<%fname = makefilename(now())%>
其實嘛,就是以年月日時分秒命名的文件。
iv,最后,生成的文件該如何查看到?當然需要把生成文件的路徑保存的數據庫中,并且添加到相對應的記錄集中了。當然,這在下面的數據庫設計時會提及到。
3,模板技術和2HTML技術的結合:將模板中特殊代碼的值替換為從表單接受過來的值,完成模板功能;將最終替換過的所有模板代碼生成HTML文件。需要注意的是:替換應能將輸入數據的格式或者支持UBB的代碼徹底改變。
二,再進行數據庫設計
目前數據庫的設計需要兩個表:一個是存放模板數據的;一個是存放信息內容的。
1,建立新數據庫asp2html.mdb
2,設計新數據庫表c_moban
字段m_id(自動編號,主關鍵字);字段m_html(備注類型)。
并將下列完整的代碼拷貝至m_html字段
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=hz">
<title>Cnbruce.Com | ASP2HTML TEST</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="2">
<tr align="right" bgcolor="#CCCCCC">
<td height="20" colspan="2">$ cntop$ </td>
</tr>
<tr valign="top">
<td width="25%" bgcolor="#e5e5e5">$ cnleft$ </td>
<td width="74%" bgcolor="#f3f3f3">$ cnright$ </td>
</tr>
</table>
</body>
</html>
3,設計新數據庫表c_news
字段c_id:自動編號,主關鍵字
字段c_title:文本類型,保存文章標題
字段c_content:備注類型,保存文章內容
字段c_filepath:文本類型,保持生成文件的路徑地址
字段c_time:日期/時間類型,默認值:Now()
三,頁面需求設計
1,首先建立一個存放HTML頁的文件夾
在文件同一目錄下,建立文件夾newsfile,夾子內部主要存放生成的HTML頁面,當然內部還會采用程序方式建立以日期命名的子文件夾,以方便瀏覽以及管理。
2,功能函數頁面lib.asp
<%
''生成文件名的函數
function makefilename(fname)
fname = fname
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename=fname & ".shtml"
end function
''保持數據格式不變的函數
function HTMLEncode(fString)
fString = replace(fString, ">", ">")
fString = replace(fString, "<", "<")
fString = Replace(fString, CHR(32), " ")
fString = Replace(fString, CHR(13), "")
fString = Replace(fString, CHR(10) & CHR(10), "<br>")
fString = Replace(fString, CHR(10), "<br>")
HTMLEncode = fString
end function
%>
3,數據庫連接頁面conn.asp
完成數據庫的字符串連接方法
<%
set conn = Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("asp2html.mdb")
conn.Open connstr
%>
4,信息輸入頁面add.html
其實很簡單:)就是表單嘛。注意action是跳轉到addit.asp
<form action="addit.asp" method="post">
Title:<input type="text" name="c_title"><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"></textarea><br>
<input type="submit" value="Add">
<input type="reset" value="Reset">
</form>
5,處理數據功能顯示頁面addit.asp
首先是處理接受過來的數據,并將值寫入數據庫;接著將模板代碼進行引用,并將其中特殊代碼轉換為接受值,最終通過FSO生成HTML頁面。其中需要注意的還有,生成文件的路徑地址保存至數據庫表。
<%''容錯處理
On Error Resume Next
%>
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%''接受傳遞值
c_title=request.form("c_title")
c_content=request.form("c_content")
%>
<%''生成HTML文件名,建立文件夾,指定文件路徑
fname = makefilename(now()) ''makefilename為自定義函數
folder = "newsfile/"&date()&"/"
filepath = folder&fname
%>
<%''將接受值及路徑保持至數據庫表
sql = "Select * from c_news"
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,3,2
rs.addnew
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_filepath")=filepath
rs.update
rs.close
Set rs = Nothing
%>
<%''打開模板代碼,并將其中特殊代碼轉變為接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$ cntop$ ",now())
mb_code=replace(mb_code,"$ cnleft$ ",c_title)
mb_code=replace(mb_code,"$ cnright$ ",c_content)
%>
<%''生成HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.CreateFolder(Server.MapPath(folder))
Set fout = fso.CreateTextFile(Server.MapPath(filepath))
fout.WriteLine mb_code
fout.close
%>
文章添加成功,<a href="/showit.asp">瀏覽</a>
6,顯示數據庫表記錄,并做指向HTML頁的鏈接:showit.asp
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%id=request.querystring("c_id")%>
<%
if request.form("submit")="change" then
c_title=request.form("c_title")
c_content=request.form("c_content")
c_id=request.form("c_id")
c_filepath=request.form("c_filepath")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news where c_id="&c_id
rs.Open sql,conn,3,2
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_time")=now()
rs.update
rs.close
Set rs = Nothing
%>
<%''打開模板代碼,并將其中特殊代碼轉變為接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$ cntop$ ",now())
mb_code=replace(mb_code,"$ cnleft$ ",c_title)
mb_code=replace(mb_code,"$ cnright$ ",c_content)
%>
<%''生成HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(c_filepath))
fout.WriteLine mb_code
fout.close
%>
<%response.redirect("showit.asp")%>
<%end if%>
<%
if id<>"" then
Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from c_news where c_id="&id
rs.Open sql,conn,1,1
c_id=rs("c_id")
c_filepath=rs("c_filepath")
c_title=rs("c_title")
c_content=rs("c_content")
end if
%>
<form action="change.asp" method="post">
Title:<input type="text" name="c_title" value=<%=c_title%>><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"><%=c_content%></textarea><br>
<input type="submit" value="change" name="submit">
<input type="reset" value="Reset">
<input name="c_id" type="hidden" value="<%=id%>">
<input name="c_filepath" type="hidden" value="<%=c_filepath%>">
</form>
7,修改數據內容頁chang.asp
修改數據內容,同時也需要修改更新對應的HTML頁面。修改其實就是重新生成文件,且文件名和之前一樣,類似文件的覆蓋。
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%id=request.querystring("c_id")%>
<%
if request.form("submit")="change" then
c_title=request.form("c_title")
c_content=request.form("c_content")
c_id=request.form("c_id")
c_filepath=request.form("c_filepath")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news where c_id="&c_id
rs.Open sql,conn,3,2
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_time")=now()
rs.update
rs.close
Set rs = Nothing
%>
<%''打開模板代碼,并將其中特殊代碼轉變為接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$ cntop$ ",now())
mb_code=replace(mb_code,"$ cnleft$ ",c_title)
mb_code=replace(mb_code,"$ cnright$ ",c_content)
%>
<%''生成HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(c_filepath))
fout.WriteLine mb_code
fout.close
%>
<%response.redirect("showit.asp")%>
<%end if%>
<%
if id<>"" then
Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from c_news where c_id="&id
rs.Open sql,conn,1,1
c_id=rs("c_id")
c_filepath=rs("c_filepath")
c_title=rs("c_title")
c_content=rs("c_content")
end if
%>
<form action="change.asp" method="post">
Title:<input type="text" name="c_title" value=<%=c_title%>><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"><%=c_content%></textarea><br>
<input type="submit" value="change" name="submit">
<input type="reset" value="Reset">
<input name="c_id" type="hidden" value="<%=id%>">
<input name="c_filepath" type="hidden" value="<%=c_filepath%>">
</form>
8,刪除記錄頁del.asp
同樣!刪除,除了刪除數據庫表中的記錄,與其對應的HTML頁面也需刪除。代碼如下:
<!--#i nclude file="conn.asp" -->
<%
c_id = request.querystring("c_id")
sql = "Select * from c_news where c_id="&c_id
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,2,3
filepath=rs("c_filepath")
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(Server.mappath(filepath))
Set fso = nothing
rs.delete
rs.close
Set rs = Nothing
conn.close
set conn=nothing
%>
<%response.redirect("showit.asp")%>
四,其它功能
模板管理頁面:
不會每次都是打開數據庫表進行增加或者修改模板代碼吧,所以,管理代碼的頁面程序不能少了,自己搗鼓下應該很簡單的。當然,之前管理員的登錄認證程序就不在書中交代了:)還有,如果設計了多個模板,那么在發表信息的時候應添加模板選擇單選框,同樣在執行轉換HTML時,SQL選擇的不同m_id了
Asp生成HTML
方法一: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()
%>
新建表moban:字段m_id(自動編號,主關鍵字);字段m_html(備注類型)
2,假設第一模板內容代碼
將下列代碼拷貝到m_html字段中
以下是代碼片段:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>testmb</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="2">
<tr align="right" bgcolor="#CCCCCC">
<td height="20" colspan="2">$ cntop$ </td>
</tr>
<tr valign="top">
<td width="25%" bgcolor="#e5e5e5">$ cnleft$ </td>
<td width="74%" bgcolor="#f3f3f3">$ cnright$ </td>
</tr>
</table>
</body>
</html>
注意$ cntop$ 、$ cnleft$ 、$ cnright$ ,它們將要實現某些具體的程序功能
3,建立數據庫連接文件conn.asp
以下是代碼片段:
<%
set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("testmb.mdb")
conn.Open connstr
%>
4,建立特殊字符串轉換所需要的庫文件lib.asp
該文件的主要作用是將實現某些功能的ASP程序做成字程序,以方便調用。
以下是代碼片段:
<%
dim topcode
sub cntop()
topcode="現在時間是:"
topcode=topcode&now()
end sub
dim leftcode,i
sub cnleft()
for i = 1 to 5
leftcode=leftcode&"<p>cnbruce.com"
next
end sub
dim rightcode
sub cnright()
for i = 1 to 9
rightcode=rightcode&"<hr color="&i&i&i&i&i&i&">"
next
end sub
%>
5,最后,調用數據庫中的模板代碼,將特殊字符串轉換。
以下是代碼片段:
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%
sql="select * from moban where m_id=1"
set rs=Server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
mb_code=rs("m_html")
rs.close
set rs=nothing
cntop()
mb_code=replace(mb_code,"$ cntop$ ",topcode)
cnleft()
mb_code=replace(mb_code,"$ cnleft$ ",leftcode)
cnright()
mb_code=replace(mb_code,"$ cnright$ ",rightcode)
response.write mb_code
%>
該頁主要作用是將模板代碼進行顯示,并將其中的特殊代碼轉變為相對應子程序功能。
至此,ASP的模板功能基本完成,剩下的就是:建立具備編輯模板功能的程序頁面,將庫文件改變為自己所需要程序功能……
2,2HTML技術又該如何實現呢?如何使得ASP頁面轉變為HTML?一般都會想到FSO組件,因為該組件能新建任何文件格式。
那么其整個運行過程是怎么樣的呢?
a,提供信息輸入頁面進行信息收集;
b,接受信息值先保存數據庫,再FSO生成文件;
c,技術性完成任務,顯示剛被創建的HTML文件的路徑地址。
該技術的實現過程中有如下幾個難點:
i,FSO生成的文件是直接放在一個大文件夾下,還是單獨放在某個每日更新的子文件夾中?可能表述不準確,這樣理解吧:相信通過FSO生成的文件隨著時間的推移,文件會越來越多,管理也會越來越亂……通常你可能看到一些地址諸如 www.xxx.com/a/2004-5-20/200405201111.html ; 可以分析得出應該是建立了當前日期的文件夾。這樣,一天就是一個文件夾的頁面內容,查看管理也就顯得比較合理。
ii,我在試圖通過以上方法建立文件夾的時候,又發現了第二個問題。第一次通過FSO建立以當前日期命名的文件夾,沒有問題。當我有新的文件需要生成時,因為是同一個程序,所以,其又將會執行建立同樣的文件夾。此時,FSO組件會發現該路徑已存在……卡殼-_-! 繼續處理,在首行添加代碼:
On Error Resume Next
嘿嘿,達到自欺欺人、掩耳盜鈴的效果。
iii,文件夾是建立了,文件該如何建立呢?主要也就是文件名的生成。當然這個就需要自己來寫個函數,功能就是如何生成文件名:)
<%
function makefilename(fname)
fname = fname ''前fname為變量,后fname為函數參數引用
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename = fname & ".html"
end function
%>
引用函數則:
<%fname = makefilename(now())%>
其實嘛,就是以年月日時分秒命名的文件。
iv,最后,生成的文件該如何查看到?當然需要把生成文件的路徑保存的數據庫中,并且添加到相對應的記錄集中了。當然,這在下面的數據庫設計時會提及到。
3,模板技術和2HTML技術的結合:將模板中特殊代碼的值替換為從表單接受過來的值,完成模板功能;將最終替換過的所有模板代碼生成HTML文件。需要注意的是:替換應能將輸入數據的格式或者支持UBB的代碼徹底改變。
二,再進行數據庫設計
目前數據庫的設計需要兩個表:一個是存放模板數據的;一個是存放信息內容的。
1,建立新數據庫asp2html.mdb
2,設計新數據庫表c_moban
字段m_id(自動編號,主關鍵字);字段m_html(備注類型)。
并將下列完整的代碼拷貝至m_html字段
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=hz">
<title>Cnbruce.Com | ASP2HTML TEST</title>
</head>
<body leftmargin="0" topmargin="0">
<table width="100%" height="100%" border="0" cellpadding="5" cellspacing="2">
<tr align="right" bgcolor="#CCCCCC">
<td height="20" colspan="2">$ cntop$ </td>
</tr>
<tr valign="top">
<td width="25%" bgcolor="#e5e5e5">$ cnleft$ </td>
<td width="74%" bgcolor="#f3f3f3">$ cnright$ </td>
</tr>
</table>
</body>
</html>
3,設計新數據庫表c_news
字段c_id:自動編號,主關鍵字
字段c_title:文本類型,保存文章標題
字段c_content:備注類型,保存文章內容
字段c_filepath:文本類型,保持生成文件的路徑地址
字段c_time:日期/時間類型,默認值:Now()
三,頁面需求設計
1,首先建立一個存放HTML頁的文件夾
在文件同一目錄下,建立文件夾newsfile,夾子內部主要存放生成的HTML頁面,當然內部還會采用程序方式建立以日期命名的子文件夾,以方便瀏覽以及管理。
2,功能函數頁面lib.asp
<%
''生成文件名的函數
function makefilename(fname)
fname = fname
fname = replace(fname,"-","")
fname = replace(fname," ","")
fname = replace(fname,":","")
fname = replace(fname,"PM","")
fname = replace(fname,"AM","")
fname = replace(fname,"上午","")
fname = replace(fname,"下午","")
makefilename=fname & ".shtml"
end function
''保持數據格式不變的函數
function HTMLEncode(fString)
fString = replace(fString, ">", ">")
fString = replace(fString, "<", "<")
fString = Replace(fString, CHR(32), " ")
fString = Replace(fString, CHR(13), "")
fString = Replace(fString, CHR(10) & CHR(10), "<br>")
fString = Replace(fString, CHR(10), "<br>")
HTMLEncode = fString
end function
%>
3,數據庫連接頁面conn.asp
完成數據庫的字符串連接方法
<%
set conn = Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("asp2html.mdb")
conn.Open connstr
%>
4,信息輸入頁面add.html
其實很簡單:)就是表單嘛。注意action是跳轉到addit.asp
<form action="addit.asp" method="post">
Title:<input type="text" name="c_title"><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"></textarea><br>
<input type="submit" value="Add">
<input type="reset" value="Reset">
</form>
5,處理數據功能顯示頁面addit.asp
首先是處理接受過來的數據,并將值寫入數據庫;接著將模板代碼進行引用,并將其中特殊代碼轉換為接受值,最終通過FSO生成HTML頁面。其中需要注意的還有,生成文件的路徑地址保存至數據庫表。
<%''容錯處理
On Error Resume Next
%>
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%''接受傳遞值
c_title=request.form("c_title")
c_content=request.form("c_content")
%>
<%''生成HTML文件名,建立文件夾,指定文件路徑
fname = makefilename(now()) ''makefilename為自定義函數
folder = "newsfile/"&date()&"/"
filepath = folder&fname
%>
<%''將接受值及路徑保持至數據庫表
sql = "Select * from c_news"
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,3,2
rs.addnew
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_filepath")=filepath
rs.update
rs.close
Set rs = Nothing
%>
<%''打開模板代碼,并將其中特殊代碼轉變為接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$ cntop$ ",now())
mb_code=replace(mb_code,"$ cnleft$ ",c_title)
mb_code=replace(mb_code,"$ cnright$ ",c_content)
%>
<%''生成HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.CreateFolder(Server.MapPath(folder))
Set fout = fso.CreateTextFile(Server.MapPath(filepath))
fout.WriteLine mb_code
fout.close
%>
文章添加成功,<a href="/showit.asp">瀏覽</a>
6,顯示數據庫表記錄,并做指向HTML頁的鏈接:showit.asp
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%id=request.querystring("c_id")%>
<%
if request.form("submit")="change" then
c_title=request.form("c_title")
c_content=request.form("c_content")
c_id=request.form("c_id")
c_filepath=request.form("c_filepath")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news where c_id="&c_id
rs.Open sql,conn,3,2
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_time")=now()
rs.update
rs.close
Set rs = Nothing
%>
<%''打開模板代碼,并將其中特殊代碼轉變為接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$ cntop$ ",now())
mb_code=replace(mb_code,"$ cnleft$ ",c_title)
mb_code=replace(mb_code,"$ cnright$ ",c_content)
%>
<%''生成HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(c_filepath))
fout.WriteLine mb_code
fout.close
%>
<%response.redirect("showit.asp")%>
<%end if%>
<%
if id<>"" then
Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from c_news where c_id="&id
rs.Open sql,conn,1,1
c_id=rs("c_id")
c_filepath=rs("c_filepath")
c_title=rs("c_title")
c_content=rs("c_content")
end if
%>
<form action="change.asp" method="post">
Title:<input type="text" name="c_title" value=<%=c_title%>><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"><%=c_content%></textarea><br>
<input type="submit" value="change" name="submit">
<input type="reset" value="Reset">
<input name="c_id" type="hidden" value="<%=id%>">
<input name="c_filepath" type="hidden" value="<%=c_filepath%>">
</form>
7,修改數據內容頁chang.asp
修改數據內容,同時也需要修改更新對應的HTML頁面。修改其實就是重新生成文件,且文件名和之前一樣,類似文件的覆蓋。
<!--#i nclude file="conn.asp" -->
<!--#i nclude file="lib.asp" -->
<%id=request.querystring("c_id")%>
<%
if request.form("submit")="change" then
c_title=request.form("c_title")
c_content=request.form("c_content")
c_id=request.form("c_id")
c_filepath=request.form("c_filepath")
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from c_news where c_id="&c_id
rs.Open sql,conn,3,2
rs("c_title")=c_title
rs("c_content")=c_content
rs("c_time")=now()
rs.update
rs.close
Set rs = Nothing
%>
<%''打開模板代碼,并將其中特殊代碼轉變為接受值
sql1="select m_id,m_html from c_moban where m_id=1"
set rs1=Server.CreateObject("adodb.recordset")
rs1.open sql1,conn,1,1
mb_code=rs1("m_html")
rs1.close
set rs1=nothing
conn.close
set conn=nothing
c_title=htmlencode(c_title)
c_content=htmlencode(c_content)
mb_code=replace(mb_code,"$ cntop$ ",now())
mb_code=replace(mb_code,"$ cnleft$ ",c_title)
mb_code=replace(mb_code,"$ cnright$ ",c_content)
%>
<%''生成HTML頁面
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(c_filepath))
fout.WriteLine mb_code
fout.close
%>
<%response.redirect("showit.asp")%>
<%end if%>
<%
if id<>"" then
Set rs = Server.CreateObject ("ADODB.Recordset")
sql="select * from c_news where c_id="&id
rs.Open sql,conn,1,1
c_id=rs("c_id")
c_filepath=rs("c_filepath")
c_title=rs("c_title")
c_content=rs("c_content")
end if
%>
<form action="change.asp" method="post">
Title:<input type="text" name="c_title" value=<%=c_title%>><br>
Content:<br>
<textarea name="c_content" rows="8" cols="30"><%=c_content%></textarea><br>
<input type="submit" value="change" name="submit">
<input type="reset" value="Reset">
<input name="c_id" type="hidden" value="<%=id%>">
<input name="c_filepath" type="hidden" value="<%=c_filepath%>">
</form>
8,刪除記錄頁del.asp
同樣!刪除,除了刪除數據庫表中的記錄,與其對應的HTML頁面也需刪除。代碼如下:
<!--#i nclude file="conn.asp" -->
<%
c_id = request.querystring("c_id")
sql = "Select * from c_news where c_id="&c_id
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,2,3
filepath=rs("c_filepath")
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(Server.mappath(filepath))
Set fso = nothing
rs.delete
rs.close
Set rs = Nothing
conn.close
set conn=nothing
%>
<%response.redirect("showit.asp")%>
四,其它功能
模板管理頁面:
不會每次都是打開數據庫表進行增加或者修改模板代碼吧,所以,管理代碼的頁面程序不能少了,自己搗鼓下應該很簡單的。當然,之前管理員的登錄認證程序就不在書中交代了:)還有,如果設計了多個模板,那么在發表信息的時候應添加模板選擇單選框,同樣在執行轉換HTML時,SQL選擇的不同m_id了
Asp生成HTML
方法一: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文件的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用SQL删除重复记录的N种方法
- 下一篇: asp获取屏幕分辨率