ASP里两种常用的生成文件的方式是:利用ADODB.Stream生成文件和利用Scripting.FileSystemObject生成文件
1、Scripting.FileSystemObject生成文件方法:
<% Set fso = CreateObject("Scripting.FileSystemObject") File=Server.MapPath("要生成文件路径和文件名.htm") Set txt=fso.OpenTextFile(File,8,True) data1="文件内容"用WriteLine方法生成文件 txt.WriteLine data1 data2="文件内容"'用Write方法生成文件 txt.Write data2 txt.Close txt.fso %> 2、ADODB.Stream生成文件方法:
<% Dim objAdoStream Set objAdoStream = Server.createObject("ADODB.Stream") objAdoStream.Type = 2 objAdoStream.Open objAdoStream.WriteText = "这是生成的内容" objAdoStream.SaveToFile Server.MapPath("NewTestFile24443.htm"),2 objAdoStream.Close() Set objAdoStream = Nothing %> 第二种方式的应用函数及实例:
<% Sub SaveAFile(ByVal sUrl,ByVal sTxt,ByVal sCharset) Dim objStream On Error Resume Next Set objStream = Server.CreateObject("ADODB.Stream") If Err.Number=-2147221005 Then Response.Write "抱歉,您的网站服务器不支持ADODB.Stream,无法生成文件" Err.Clear Response.End End If With objStream .Type = 2 .Open .Charset = sCharset .Position = objStream.Size .WriteText = sTxt .SaveToFile sUrl,2 .Close End With Set objStream = Nothing End Sub '应用实例 SaveAFile Server.MapPath("MyTestFile.htm"),"您好,我是CNLEI.","gb2312" %> |