王新阳

wangxinyang

ASP生成utf-8编码的文本文件

直接调用fso.OpenTextFile方法生成的文本文件为ANSI编码,要生成utf-8编码的文本文件需要用到Adodb.Stream组件(主要用于生成utf-8编码的html文件)

Sub toTxt(ByVal f, ByVal s)
	Dim adoStream, fso
	Set fso=Server.CreateObject("Scripting.FileSystemObject")
	If InStr(f, "/")>0 Then
		If Not fso.FolderExists(Server.MapPath(Left(f, InStrRev(f, "/")))) Then
			Set fso=Nothing
			Exit Sub
		End If
	End If
	Set adoStream = Server.CreateObject("ADODB.Stream")
	If Err.Number=-2147221005 Then Exit Sub	'不支持Adodb.Stream组件
	With adoStream
		.Type = 2
		.Open
		.Charset  = "utf-8"
		.Position = .Size
		.WriteText= s
		.SaveToFile Server.MapPath(f), 2
		.Close
	End With
	Set adoStream = Nothing
End Sub
2015-08-17
2024-11-23 星期六 农历十月二十三