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

2025-10-13 20:29:38
推荐回答(5个)
回答1:

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

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>




无标题文档



<%
'使用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
%>


文件内容:






回答2:

<%
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
%>

回答3:

<%
'下面是保存函数
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(文本内容,文件路径)
剩下的自己会写了吧

回答4:

<%
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
%>

回答5:

既然非常简单,我就不用说了。呵呵