要生成文件肯空要用到FSO(FileSystemObject)組件,通過asp生成靜態網頁主要有兩種方式:
a、生成的內容由多部分連接而成;
b、生成的內容基於模板生成。
2)方式1:生成的內容由多部分連接而成
步驟:
a、設計要輸出網頁的布局
b、設計生成HTML的asp文件
例子:
輸出網頁的布局:
<html>
<head>
<title>標題</title>
<style type="text/css">
<!--
.article_title {
font-size: 22px;
font-weight: bold;
text-align: center;
padding-top: 10px;
padding-bottom: 20px;
}
.content {
text-indent: 18px;
font-size: 16px;
line-height: 230%;
text-align: left;
}
.from {
font-size: 14px;
text-align: right;
padding-right: 15px;
padding-top: 15px;
}
.feature_bar {
font-size: 14px;
color: #999999;
text-align: center;
padding-bottom: 15px;
}
-->
</style>
</head>
<body>
<table width="80%">
<tr>
<td><div class="article_title">標題</div>
<div class="feature_bar">作者: 錄入時間: 錄入: </div>
<div class="content">內容</div>
<div class="from">來源:</div> </td>
</tr>
</table>
</body>
</html>
把源代碼中的所有的 " 替換成 "",作用是在ASP中輸出雙引號。
設計asp文件:
<% Option Explicit %>
<html>
<head>
<title>ASP生成HTML</title>
<style type="text/css">
<!--
.align_right_top {
text-align: right;
vertical-align: top;
}
.align_left_10px {
text-align: left;
padding-left: 10px;
}
-->
</style>
</head>
<body>
<form method="post" action="?action=create">
<table width="80%">
<tr>
<td class="align_right_top" >HTML文件名稱:</td>
<td class="align_left_10px"><input name="HtmlFileName" type="text" id="HtmlFileName" /></td>
</tr>
<tr>
<td class="align_right_top" >文章標題:</td>
<td class="align_left_10px"><input name="title" type="text" id="title" /></td>
</tr>
<tr>
<td class="align_right_top">作者:</td>
<td class="align_left_10px"><input name="author" type="text" id="author" /></td>
</tr>
<tr>
<td class="align_right_top">錄入:</td>
<td class="align_left_10px"><input name="editor" type="text" id="editor" /></td>
</tr>
<tr>
<td class="align_right_top">輸入時間:</td>
<td class="align_left_10px"><input name="EditTime" type="text" id="EditTime" /></td>
</tr>
<tr>
<td class="align_right_top">文章內容:</td>
<td class="align_left_10px"><textarea name="content" cols="55" rows="20" id="content"></textarea></td>
</tr>
<tr>
<td class="align_right_top">來源:</td>
<td class="align_left_10px"><input name="from" type="text" id="from" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="Submit" value="提交" /></td>
</tr>
</table>
</form>
<%
if Trim(Request.QueryString("action"))="create" then
dim title , author , editor , EditTime , content , from , html
title=Trim(Request.Form("title"))
editor=Trim(Request.Form("editor"))
EditTime=Trim(Request.Form("EditTime"))
content=Trim(Request.Form("content"))
from=Trim(Request.Form("from"))
html="<html>"_ '粘貼上面的修改後的 輸出網頁布局的源代碼
&"<head>"_ ‘並用 _ 與 & 把各行連接起來 或刪除多余空格使源代碼寫在壹行
&"<title>"&title&"</title>"_
&"<style type=""text/css"">"_
&".article_title {"_
&"font-size: 22px;"_
&"font-weight: bold;"_
&"text-align: center;"_
&"padding-top: 10px;"_
&"padding-bottom: 20px;"_
&"}"_
&".content {"_
&"text-indent: 18px;"_
&"font-size: 16px;"_
&"line-height: 230%;"_
&"text-align: left;"_
&"}"_
&".from {"_
&"font-size: 14px;"_
&"text-align: right;"_
&"padding-right: 15px;"_
&"padding-top: 15px;"_
&"}"_
&".feature_bar {"_
&"font-size: 14px;"_
&"color: #999999;"_
&"text-align: center;"_
&"padding-bottom: 15px;"_
&"}"_
&"</style>"_
&"</head>"_
&"<body> "_
&"<table width=""80%"">"_
&"<tr>"_
&"<td><div class=""article_title"">"&title&"</div>"_
&"<div class=""feature_bar"">作者:"&author&" 錄入時間:"&EditTime&" 錄入:"&editor&" </div>"_
&"<div class=""content"">"&content&"</div>"_
&"<div class=""from"">來源:"&from&"</div> </td>"_
&"</tr>"_
&"</table>"_
&"</body> "_
&"</html>"
dim HtmlFileName ,HtmlFile , fs , FileStream
HtmlFileName=Trim(Request.Form("HtmlFileName"))
if instr(HtmlFileName,".html")=false then
HtmlFileName="NoName.html"
end if
HtmlFile=Server.MapPath(HtmlFileName)
set fs=CreateObject("Scripting.FileSystemObject")
set FileStream=fs.CreateTextFile(HtmlFile)
FileStream.WriteLine Html
FileStream.close
set FileStream=nothing
response.Write("<script>alert('生成"&HtmlFileName&"文件成功!');history.go(-1);</script>")
end if
%>
</body>
</htm>
把上面的asp文件保存放到服務器上即可運行
3)方式2:生成的內容基於模板生成
思想:
給模板asp傳遞參數,使用“MSXML2.XMLHTTP”讀取基於參數傳遞的asp模板的網頁源代碼,
再使用FSO組件生成靜態網頁。
步驟:
a、設計有參數傳遞的asp模板
b、設計asp控制頁
設計asp模板:(保存成template.asp)
<% Option Explicit %>
<%
dim HtmlFileName ,title , author , editor ,EditTime ,content ,from
HtmlFileName=Trim(Request.QueryString("HtmlFileName"))
title=Trim(Request.QueryString("title"))
author=Trim(Request.QueryString("author"))
editor=Trim(Request.QueryString("editor"))
EditTime=Trim(Request.QueryString("EditTime"))
content=Trim(Request.QueryString("content"))
from=Trim(Request.QueryString("from"))
%>
<html>
<head>
<title><%= title %></title>
<style type="text/css">
<!--
.article_title {
font-size: 22px;
font-weight: bold;
text-align: center;
padding-top: 10px;
padding-bottom: 20px;
}
.content {
text-indent: 18px;
font-size: 16px;
line-height: 230%;
text-align: left;
}
.from {
font-size: 14px;
text-align: right;
padding-right: 15px;
padding-top: 15px;
}
.feature_bar {
font-size: 14px;
color: #999999;
text-align: center;
padding-bottom: 15px;
}
-->
</style>
</head>
<body>
<table width="80%">
<tr>
<td><div class="article_title"><%= title %></div>
<div class="feature_bar">作者:<%= author %> 錄入時間:<%= EditTime %> 錄入:<%= editor %> </div>
<div class="content"><%= content %></div>
<div class="from">來源:<%= from %></div> </td>
</tr>
</table>
</body>
</html>
設計asp文件:(保存成html.asp)
<% Option Explicit %>
<html>
<head>
<title>ASP生成HTML</title>
<style type="text/css">
<!--
.align_right_top {
text-align: right;
vertical-align: top;
}
.align_left_10px {
text-align: left;
padding-left: 10px;
}
-->
</style>
</head>
<body>
<form method="post" action="?action=create">
<table width="80%">
<tr>
<td class="align_right_top" >HTML文件名稱:</td>
<td class="align_left_10px"><input name="HtmlFileName" type="text" id="HtmlFileName" /></td>
</tr>
<tr>
<td class="align_right_top" >文章標題:</td>
<td class="align_left_10px"><input name="title" type="text" id="title" /></td>
</tr>
<tr>
<td class="align_right_top">作者:</td>
<td class="align_left_10px"><input name="author" type="text" id="author" /></td>
</tr>
<tr>
<td class="align_right_top">錄入:</td>
<td class="align_left_10px"><input name="editor" type="text" id="editor" /></td>
</tr>
<tr>
<td class="align_right_top">輸入時間:</td>
<td class="align_left_10px"><input name="EditTime" type="text" id="EditTime" /></td>
</tr>
<tr>
<td class="align_right_top">文章內容:</td>
<td class="align_left_10px"><textarea name="content" cols="55" rows="20" id="content"></textarea></td>
</tr>
<tr>
<td class="align_right_top">來源:</td>
<td class="align_left_10px"><input name="from" type="text" id="from" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="Submit" value="提交" /></td>
</tr>
</table>
</form>
<%
function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
end function
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
%>
<%
if Trim(Request.QueryString("action"))="create" then
dim title , author , editor , EditTime , content , from , html
title=Trim(Request.Form("title"))
editor=Trim(Request.Form("editor"))
EditTime=Trim(Request.Form("EditTime"))
content=Trim(Request.Form("content"))
from=Trim(Request.Form("from"))
'讀取傳遞參數後的模版源代碼,地址根據具體情況而定
html=getHTTPPage("http://127.0.0.1/template.asp"_
&"?title="&title&"&editor="&editor&"&EditTime="_
&EditTime&"&content="&content&"&from="&content&"")
dim HtmlFileName ,HtmlFile , fs , FileStream
HtmlFileName=Trim(Request.Form("HtmlFileName"))
if instr(HtmlFileName,".html")=false then
HtmlFileName="NoName.html"
end if
HtmlFile=Server.MapPath(HtmlFileName)
set fs=CreateObject("Scripting.FileSystemObject")
set FileStream=fs.CreateTextFile(HtmlFile)
FileStream.WriteLine Html
FileStream.close
set FileStream=nothing
response.Write("<script>alert('生成"&HtmlFileName&"文件成功!');history.go(-1);</script>")
end if
%>
</body>
</htm>
把template.asp與html.asp 放在同壹目錄通過服務器運行後即可。
4)結論
通過比較可以看出,通過方式壹生成的html文件源代碼比較亂,而通過模板生成的html文件源代碼跟原先模板的源代碼壹致。