本文将通过一个具体的示例,介绍如何使用ASP(Active Server Pages)技术动态生成HTML文件。该示例包括两个页面:首页index.htm和处理页面send.asp。
首页index.htm是一个简单的表单页面,用户可以通过这个表单输入信息并提交。处理页面send.asp负责接收表单数据,并将其保存为HTML文件。
以下是详细的代码实现:
首页index.htm:
处理页面send.asp:
<%
' 转换日期时间函数
Function chan_time(shijian)
Dim s_year, s_month, s_day, s_hour, s_minute
s_year = Year(shijian)
If Len(s_year) = 2 Then s_year = "20" & s_year
s_mOnth= Month(shijian)
If s_month <10 Then s_mOnth= "0" & s_month
s_day = Day(shijian)
If s_day <10 Then s_day = "0" & s_day
s_hour = Hour(shijian)
If s_hour <10 Then s_hour = "0" & s_hour
s_minute = Minute(shijian)
If s_minute <10 Then s_minute = "0" & s_minute
chan_time = s_year & s_month & s_day & s_hour & s_minute
End Function
' 转换日期函数
Function chan_data(shijian)
Dim s_year, s_month, s_day
s_year = Year(shijian)
If Len(s_year) = 2 Then s_year = "20" & s_year
s_mOnth= Month(shijian)
If s_month <10 Then s_mOnth= "0" & s_month
s_day = Day(shijian)
If s_day <10 Then s_day = "0" & s_day
chan_data = s_year & s_month & s_day
End Function
' 转换文件名函数
Function chan_file(shijian)
Dim s_month, s_day, s_hour, s_minute, s_ss
s_mOnth= Month(shijian)
If s_month <10 Then s_mOnth= "0" & s_month
s_day = Day(shijian)
If s_day <10 Then s_day = "0" & s_day
s_hour = Hour(shijian)
If s_hour <10 Then s_hour = "0" & s_hour
s_minute = Minute(shijian)
If s_minute <10 Then s_minute = "0" & s_minute
s_ss = Second(shijian)
If s_ss <10 Then s_ss = "0" & s_ss
chan_file = s_month & s_day & s_hour & s_minute & s_ss
End Function
Dim top, botom, msg, fs, all_tree2, pass, file1, files, filez, ts, MyFile, fdir
' 获取表单数据
msg = Request.Form("msg")
msg = Replace(msg, vbCrLf, "")
msg = Replace(msg, Chr(9), "")
msg = Replace(msg, " ", " ")
msg = Replace(msg, "\r\n", "
")
msg = Replace(msg, "\n", "
")
top = ""
botom = "
"
msg = top & msg & botom
' 创建文件系统对象
Set fs = Server.CreateObject("Scripting.FileSystemObject")
all_tree2 = Server.MapPath("news") & "\" & chan_data(Now)
' 判断文件夹是否存在,如果不存在则创建
If Not fs.FolderExists(all_tree2) Then
fs.CreateFolder(all_tree2)
End If
' 生成文件名
pass = chan_file(Now)
Randomize ' 使用系统计时器初始化随机数生成器
pass = Rnd(pass)
pass = get_pass(pass)
pass = Left(pass, 10)
file1 = pass
files = file1 & ".txt"
filez = all_tree2 & "\" & files
' 写入文件
Set ts = fs.CreateTextFile(filez, True)
For z = 1 To Len(msg)
write_now = Mid(msg, z, 1)
ts.Write(write_now)
Next
ts.Close
Set ts = Nothing
' 处理错误
If Err.Number <> 0 Or Err Then
Response.Write("")
Else
Response.Write("")
Response.Write("")
End If
' 将文件扩展名改为.htm
Set MyFile = fs.GetFile(filez)
MyFile.Name = Left(MyFile.Name, Len(MyFile.Name) - 4) & ".htm"
Set MyFile = Nothing
Set fs = Nothing
Set fdir = Nothing
' 清理特殊字符
Function get_pass(pass)
pass = CStr(pass)
pass = Replace(pass, " ", "")
pass = Replace(pass, "-", "")
pass = Replace(pass, ":", "")
pass = Replace(pass, ".", "")
pass = Replace(pass, "+", "")
pass = Replace(pass, "_", "")
pass = Replace(pass, "<", "")
pass = Replace(pass, ">", "")
pass = Replace(pass, "!", "")
pass = Replace(pass, "@", "")
pass = Replace(pass, "#", "")
pass = Replace(pass, "$", "")
pass = Replace(pass, "%", "")
pass = Replace(pass, "^", "")
pass = Replace(pass, "&", "")
pass = Replace(pass, "*", "")
pass = Replace(pass, "(", "")
pass = Replace(pass, ")", "")
pass = Replace(pass, "=", "")
pass = Replace(pass, "\", "")
pass = Replace(pass, "/", "")
pass = Replace(pass, "|", "")
get_pass = pass
End Function
%>