日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

asp自动生成html文件的方法

發(fā)布時間:2025/3/21 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp自动生成html文件的方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1,設計數(shù)據(jù)庫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$ ,它們將要實現(xiàn)某些具體的程序功能

3,建立數(shù)據(jù)庫連接文件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

該文件的主要作用是將實現(xiàn)某些功能的ASP程序做成字程序,以方便調用。
以下是代碼片段:
<%
dim topcode
sub cntop()
topcode="現(xiàn)在時間是:"
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,最后,調用數(shù)據(jù)庫中的模板代碼,將特殊字符串轉換。
以下是代碼片段:
<!--#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
%>
該頁主要作用是將模板代碼進行顯示,并將其中的特殊代碼轉變?yōu)橄鄬映绦蚬δ堋?

至此,ASP的模板功能基本完成,剩下的就是:建立具備編輯模板功能的程序頁面,將庫文件改變?yōu)樽约核枰绦蚬δ堋?


2,2HTML技術又該如何實現(xiàn)呢?如何使得ASP頁面轉變?yōu)镠TML?一般都會想到FSO組件,因為該組件能新建任何文件格式。

那么其整個運行過程是怎么樣的呢?
a,提供信息輸入頁面進行信息收集;
b,接受信息值先保存數(shù)據(jù)庫,再FSO生成文件;
c,技術性完成任務,顯示剛被創(chuàng)建的HTML文件的路徑地址。

該技術的實現(xiàn)過程中有如下幾個難點:

i,FSO生成的文件是直接放在一個大文件夾下,還是單獨放在某個每日更新的子文件夾中?可能表述不準確,這樣理解吧:相信通過FSO生成的文件隨著時間的推移,文件會越來越多,管理也會越來越亂……通常你可能看到一些地址諸如 www.xxx.com/a/2004-5-20/200405201111.html ; 可以分析得出應該是建立了當前日期的文件夾。這樣,一天就是一個文件夾的頁面內容,查看管理也就顯得比較合理。

ii,我在試圖通過以上方法建立文件夾的時候,又發(fā)現(xiàn)了第二個問題。第一次通過FSO建立以當前日期命名的文件夾,沒有問題。當我有新的文件需要生成時,因為是同一個程序,所以,其又將會執(zhí)行建立同樣的文件夾。此時,FSO組件會發(fā)現(xiàn)該路徑已存在……卡殼-_-! 繼續(xù)處理,在首行添加代碼:

On Error Resume Next


嘿嘿,達到自欺欺人、掩耳盜鈴的效果。

iii,文件夾是建立了,文件該如何建立呢?主要也就是文件名的生成。當然這個就需要自己來寫個函數(shù),功能就是如何生成文件名:)


<%
function makefilename(fname)
fname = fname ''前fname為變量,后fname為函數(shù)參數(shù)引用
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
%>

引用函數(shù)則:
<%fname = makefilename(now())%>

其實嘛,就是以年月日時分秒命名的文件。

iv,最后,生成的文件該如何查看到?當然需要把生成文件的路徑保存的數(shù)據(jù)庫中,并且添加到相對應的記錄集中了。當然,這在下面的數(shù)據(jù)庫設計時會提及到。

3,模板技術和2HTML技術的結合:將模板中特殊代碼的值替換為從表單接受過來的值,完成模板功能;將最終替換過的所有模板代碼生成HTML文件。需要注意的是:替換應能將輸入數(shù)據(jù)的格式或者支持UBB的代碼徹底改變。

二,再進行數(shù)據(jù)庫設計

目前數(shù)據(jù)庫的設計需要兩個表:一個是存放模板數(shù)據(jù)的;一個是存放信息內容的。

1,建立新數(shù)據(jù)庫asp2html.mdb

2,設計新數(shù)據(jù)庫表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,設計新數(shù)據(jù)庫表c_news

字段c_id:自動編號,主關鍵字
字段c_title:文本類型,保存文章標題
字段c_content:備注類型,保存文章內容
字段c_filepath:文本類型,保持生成文件的路徑地址
字段c_time:日期/時間類型,默認值:Now()


三,頁面需求設計

1,首先建立一個存放HTML頁的文件夾

在文件同一目錄下,建立文件夾newsfile,夾子內部主要存放生成的HTML頁面,當然內部還會采用程序方式建立以日期命名的子文件夾,以方便瀏覽以及管理。

2,功能函數(shù)頁面lib.asp


<%
''生成文件名的函數(shù)
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

''保持數(shù)據(jù)格式不變的函數(shù)
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,數(shù)據(jù)庫連接頁面conn.asp
完成數(shù)據(jù)庫的字符串連接方法


<%
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,處理數(shù)據(jù)功能顯示頁面addit.asp
首先是處理接受過來的數(shù)據(jù),并將值寫入數(shù)據(jù)庫;接著將模板代碼進行引用,并將其中特殊代碼轉換為接受值,最終通過FSO生成HTML頁面。其中需要注意的還有,生成文件的路徑地址保存至數(shù)據(jù)庫表。

<%''容錯處理
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為自定義函數(shù)
folder = "newsfile/"&date()&"/"
filepath = folder&fname
%>

<%''將接受值及路徑保持至數(shù)據(jù)庫表
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
%>

<%''打開模板代碼,并將其中特殊代碼轉變?yōu)榻邮苤?
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,顯示數(shù)據(jù)庫表記錄,并做指向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
%>

<%''打開模板代碼,并將其中特殊代碼轉變?yōu)榻邮苤?
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,修改數(shù)據(jù)內容頁chang.asp

修改數(shù)據(jù)內容,同時也需要修改更新對應的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
%>

<%''打開模板代碼,并將其中特殊代碼轉變?yōu)榻邮苤?
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

同樣!刪除,除了刪除數(shù)據(jù)庫表中的記錄,與其對應的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")%>


四,其它功能

模板管理頁面:

不會每次都是打開數(shù)據(jù)庫表進行增加或者修改模板代碼吧,所以,管理代碼的頁面程序不能少了,自己搗鼓下應該很簡單的。當然,之前管理員的登錄認證程序就不在書中交代了:)還有,如果設計了多個模板,那么在發(fā)表信息的時候應添加模板選擇單選框,同樣在執(zhí)行轉換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)???????????????'打開成可以在結尾寫入數(shù)據(jù)的文件
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/這三個動態(tài)
頁面,分別生成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'寫入數(shù)據(jù)到stream
?Astream.SaveToFile?recfilen,2'保存到文件
end?sub

function?dts()'產(chǎn)生隨機文件名
?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文件的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。