FSO写UTF-8编码文件

Posted on August 19, 2006 by Fdream

昨天给aw写一个生成天气预报的xml文件的程序,考虑到通用性,于是选择utf-8编码,我也比较偏好这个编码。最先想到用FSO来写这个xml文件,后来却发现生成的xml文件不能正常显示,仅仅是因为编码的原因。用记事本打开生成的xml文件,另存为utf-8编码的xml文件即可正常显示。于是在网上搜索了一下,结果发现很多人说FSO只能写ANSI编码的文件,不能写utf-8格式的文件,并且只能读写文本文件。很多人都选择用ADODB.STREAM对象来替代FSO对象,因为STREAM类有LOADFROMFILE和SAVETOFILE方法.并且有一个至关重要的属性CHARSET,这是FSO没有的。有人给出了样例程序:

‘————————————————-
‘函数名称:ReadTextFile
‘作用:利用AdoDb.Stream对象来读取UTF-8格式的文本文件
‘—————————————————-
Function ReadFromTextFile (FileUrl,CharSet)
dim str
set stm=server.CreateObject(“adodb.stream”)
stm.Type=2 ‘以本模式读取
stm.mode=3
stm.charset=CharSet
stm.open
stm.loadfromfile server.MapPath(FileUrl)
str=stm.readtext
stm.Close
set stm=nothing
ReadFromTextFile=str
End Function

‘————————————————-
‘函数名称:WriteToTextFile
‘作用:利用AdoDb.Stream对象来写入UTF-8格式的文本文件
‘—————————————————-
Sub WriteToTextFile (FileUrl,byval Str,CharSet)
set stm=server.CreateObject(“adodb.stream”)
stm.Type=2 ‘以本模式读取
stm.mode=3
stm.charset=CharSet
stm.open
stm.WriteText str
stm.SaveToFile server.MapPath(FileUrl),2
stm.flush
stm.Close
set stm=nothing
End Sub

有这个样例程序,很轻松地搞定了生成utf-8编码的xml文件的问题。谁知一上传到服务器,访问这个页面居然显示为空白,所有的代码均没有执行,包括普通的HTML代码都不显示。后来经过一步一步测试,发现是下面这一行的问题:

stm.SaveToFile server.MapPath(FileUrl),2

更为诡异的是即使把这一行注释掉,这个页面依然不能执行,但是一旦删除这一行,整个页面就恢复正常了,估计是主机的原因……没办法咯,还是得用FSO,反正主机是支持FSO的,我还就不信FSO只能写ANSI编码的文件了!翻了一些电子书和大量网页,终于发现FSO是可以设置编码的:

object.CreateTextFile(filename[, overwrite[, unicode]])

其中unicode参数可以为以下几种:

-2 以系统默认格式打开文件。
-1 以 Unicode 格式打开文件。
0 以 ASCII 格式打开文件。

于是问题迎刃而解,上传到主机,测试,一切OK,哇哈哈~

分享 |
Categories:
Ajax Web
Tags:
, ,
Comments:
10 Comments
Views:
25,247 Views

Related Posts

10 Responses to <FSO写UTF-8编码文件>

  1. HotHeart says:

    不错,可以用FSO来生成静态页面了^_^

  2. 密陀僧 says:

    HOHO!偶一直都用上面的两个函数读写文件
    俺懒啊懒[smile]

  3. HotHeart says:

    把那啥圈圈去掉了?[question]

  4. Fdream says:

    嗯啦,玩够了^_^

  5. aw says:

    haha , 辛苦了辛苦了~~

  6. 子乌 says:

    小汗一下……我记得fso的reference里面对这个参数就说明了啊……

  7. 幸福的子弹 says:

    今天从你这里看到这篇文章,解决了我的实际中的问题,谢谢!:)

  8. 幸福的子弹 says:

    写UTF-8文件时我添加的中文注释会出现乱码:
    < ?
    ……
    stm.WriteText “companyName=”" “&request.form(“name”)&” “” ‘公司简介”
    ……
    %>

    打开文件发现我添加的’公司简介会成乱码,而生成的companyName=”南方互联” 却又是正常的。不知道为什么?

  9. 大家可以参照我的试试,想写生什么编都行.
    欢迎到本人 网站去座座.
    http://www.28oc.com

  10. ‘================================================
    ‘函数名:CreatedTextFile
    ‘作 用:创建文本文件
    ‘参 数:FileName
    ‘================================================
    Public Function CreatedTextFile(ByVal FileName, ByVal body)
    On Error Resume Next
    If InStr(FileName, “:”) = 0 Then FileName = Server.MapPath(FileName)
    Dim oStream
    Set oStream = CreateObject(“ADODB.Stream”)
    oStream.Type = 2 ‘设置为可读可写
    oStream.Mode = 3 ‘设置内容为文本
    oStream.Charset = “UTF-8″ ‘设置编码
    oStream.Open
    oStream.Position = oStream.Size
    oStream.WriteText body
    oStream.SaveToFile FileName, 2
    oStream.Close
    Set oStream = Nothing
    If Err.Number <> 0 Then Err.Clear
    End Function
    ‘================================================
    ‘函数名:Readfile
    ‘作 用:读取文件内容
    ‘参 数:fromPath —-来源文件路径
    ‘================================================
    Public Function Readfile(ByVal fromPath)
    On Error Resume Next
    Dim strTemp,fso,f
    If InStr(fromPath, “:”) = 0 Then fromPath = Server.MapPath(fromPath)
    Set fso = Server.CreateObject(FSO_ScriptName)
    If fso.FileExists(fromPath) Then
    Set f = fso.OpenTextFile(fromPath, 1, True)
    strTemp = f.ReadAll
    f.Close
    Set f = Nothing
    End If
    Set fso = Nothing
    Readfile = strTemp
    If Err.Number <> 0 Then Err.Clear
    End Function
    ‘================================================

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>