The solution of the FSO Generation UTF-8 encoding file

Source: Internet
Author: User
Tags format object chr end
Yesterday to the AW write a generation of weather forecast XML file program, taking into account versatility, so choose Utf-8 encoding, I also prefer this encoding. The first thought of using the FSO to write this XML file, but later found that the generated XML file can not be displayed normally, simply because of the encoding reasons. Open the generated XML file in Notepad, and save as a utf-8 encoded XML file to display correctly. So in search of the Internet, the results found that many people said that the FSO can only write ANSI-encoded files, can not write utf-8 format files, and can only read and write text files. Many people choose to replace the FSO object with a ADODB.stream object because the stream class has LoadFromFile and SaveToFile methods. And there is a crucial attribute charset, which is not in the FSO. Someone has given the sample program:

'-------------------------------------------------
' Function name: ReadTextFile
' Function: Use the ADODB.stream object to read text files in UTF-8 format
'----------------------------------------------------
Function Readfromtextfile (Fileurl,charset)
Dim str
Set Stm=server. CreateObject ("ADODB.stream")
Stm. type=2 ' read in this mode
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
'-------------------------------------------------
' Function name: writetotextfile
' Function: Use ADODB.stream object to write text files in UTF-8 format
'----------------------------------------------------
Sub writetotextfile (Fileurl,byval str,charset)
Set Stm=server. CreateObject ("ADODB.stream")
Stm. type=2 ' read in this mode
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

With this sample program, it's easy to fix the problem of generating UTF-8 encoded XML files. Behold a upload to the server, access to this page is actually displayed as blank, all the code is not implemented, including ordinary HTML code does not show. Later, after a step-by-step test, the problem was found in the following line:

Stm. SaveToFile server. MapPath (FILEURL), 2
What's even more bizarre is that even if this line is commented out, the page still cannot be executed, but once this line is deleted, the entire page is back to normal, presumably the reason for the host ... No way, or to use FSO, anyway, the host is to support FSO, I also do not believe that the FSO can only write ANSI-encoded files! Turned over some e-books and a lot of web pages, finally found that the FSO can be set to encode:
Object. CreateTextFile (filename[, overwrite[, Unicode])

The Unicode parameters can be as follows:

-2 ' opens the file in the system default format.
-1 ' opens file in Unicode format.
0 ' opens the file in ASCII format.

So the problem solved, uploaded to the mainframe, testing, everything OK, wow haha ~

=============== Stream traverse each line ================== = =
No such code has been processed, but the stream stream should be used alone.

Dim St
Dim c
Dim strhtml
st = Server.CreateObject ("ADODB.stream")
St. Type = 2
St. Open
St. Position = 0
St.loadfromfile Server.MapPath ("Christ.txt")
c = St.readtext
St.close
c = Split (C,CHR) &CHR (10))
For each strhtml in C
strHTML = Trim (strhtml)
strHTML = replace (strhtml, "'", "")
......
Next

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.