asp生成一个文本文件(非常简单)

一个文本输入框
提交文本内容之后在当前目录生成一个123.txt
123.txt里面的内容就是输入的内容。
如果本身就存在123.txt 就覆盖之前的123.txt

asp创建文件有两种方法,可以用fso,也可以用stream,这里给你一个stream的例子
=====代码=======

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<%
'使用stream对象输出内容至文件
Function SaveTOFile(ByVal FileName,ByRef Content,ByVal Chrset)
On Error Resume Next
dim stm:set stm=Server.CreateObject("ADODB.Stream")
stm.Type=2
stm.Mode=3
stm.CharSet=Chrset
stm.Open
stm.WriteText content
stm.SaveToFile Server.MapPath(FileName),2
stm.Flush
stm.Close
Set stm=Nothing
If Err Then
WriteTOFile = False
Else
WriteTOFile = True
End If
End Function
If Request.Form<>"" Then
SaveTOFile "123.txt",Request.Form("text"),"gb2312"
Response.Write("文件保存成功!")
End If
%>
<form method="post">
文件内容:
<textarea name="text">
</textarea>
<br />
<input type="submit" value="提交" />
</form>
</body>
</html>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-06-07
<%
Dim fso,htmlwrite
Dim strTitle,strContent,strOut
'// 创建文件系统对象
Set fso=Server.CreateObject("Scripting.FileSystemObject")
if fso.FileExists(server.MapPath("123.txt")) then
fso.deleteFile(server.MapPath(("123.txt"))
end if
strContent=request.form("content")
strOut=strContent
fileName = "123.txt"
Set htmlwrite=fso.CreateTextFile(Server.MapPath(fileName),true)
'// 写入内容
htmlwrite.WriteLine strOut
'// 关闭
htmlwrite.close
'// 释放文件系统对象
set htmlwrite=Nothing
set fso=Nothing
%>
第2个回答  推荐于2018-04-21
<%
'下面是保存函数
Function SaveToFile(ByVal strBody, ByVal File)
Dim objStream
Dim RText
RText = Array(0, "")
Set objStream = Server.CreateObject("ADODB.Stream")
With objStream
.Type = 2
.Open
.Charset = "utf-8"
.Position = objStream.Size
.WriteText = strBody
On Error Resume Next
.SaveToFile Server.MapPath(File), 2
If Err Then
RText = Array(Err.Number, Err.Description)
SaveToFile = RText
Err.Clear
Exit Function
End If
.Close
End With
RText = Array(0, "保存文件成功!")
SaveToFile = RText
Set objStream = Nothing
End Function

%>
调用 SaveToFile(文本内容,文件路径)
剩下的自己会写了吧本回答被网友采纳
第3个回答  2011-06-07
<%
txtcontent=request.form("content")
PromotionPath = "123.txt"
WriteToHtml PromotionPath,txtcontent

Function WriteToHtml(Fpath,Templet)
Dim FSO,FCr
Set FSO = CreateObject("Scripting.FileSystemObject")
if FSO.FILEExists(Fpath) then
FSO.deleteFILE Fpath
end if

Set FCr = FSO.CreateTextFile(Server.MapPath(Fpath), True)
FCr.Write(Templet)
FCr.Close
Set FCr = Nothing
Set FSO = Nothing
End Function
%>
第4个回答  2011-06-07
既然非常简单,我就不用说了。呵呵