According to the default encoding format of the web page file generated by FSO and the conversion method to UTF-8 encoding, the Adodb.Stream object is used to write the function and the UTF-8 web page file is successfully generated. The following functions use FSO objects, and the file location is FSO.ASP. There are only three file encoding attributes of the FSO object. The system default is Unicode, ASCII, and there is no UTF-8 we want. Therefore, generally the files generated by using the FSO object on the Chinese system are in the gb2312 web page encoding format, and UTF-8 encoding cannot be generated. , therefore, Latin languages such as English and Chinese can be displayed normally, but non-Latin languages such as Russian will cause garbled pages.
Copy the code code as follows:
function createfile(sfilename,scontent)
set fso=server.CreateObject(scripting.filesystemobject)
'set f1=fso.opentextfile(sfilename,2,true,-1) 'append=8 only write=2 Unicode encoding=-1
set f1=fso.opentextfile(sfilename,2,true)
f1.write(scontent)
f1.close
set fso=nothing
end function
Choose to use the ADODB.STREAM object to replace the FSO object, because the STREAM class has LOADFROMFILE and SAVETOFILE methods, and has a crucial attribute CHARSET, which FSO does not have. The following function is written in Adodb.Stream and successfully generates UTF-8 web page files.
Copy the code code as follows:
function createfile(sfilename,scontent)
Set objStream = Server.CreateObject(ADODB.Stream)
With objStream
.Open
.Charset = utf-8
.Position = objStream.Size
.WriteText=scontent
.SaveToFile sfilename,2
.Close
End With
Set objStream = Nothing
end function
For programs using FSO, as long as the function is modified and the function name remains unchanged, it can run normally, which is more trouble-free and convenient.
If you use a template to generate files, you also need to read the template file in UTF-8 encoding. Otherwise, the correct file encoding will be released in the background, but the template file is read in with FSO's GB2312 encoding. If the template page is in Russian or other non-Latin languages, it will Garbled characters appear. The function is modified as follows:
The READFILE function of FSO originally used
Copy the code code as follows:
function readfile(sfilename)
Set fso=server.CreateObject(scripting.filesystemobject)
Set f = fso.OpenTextFile(sfilename, 1, true)
if not f.AtEndOfStream then readfile = f.readAll
Set f=nothing
Set fso=nothing
end function
Replace the READFILE function of ADODB.STREAM
Note that according to actual needs, remove or retain the Function readfile (sfilename, charset) charset parameter charset.
Copy the code code as follows:
Function readfile(sfilename)
f
Set stm=server.CreateObject(adodb.stream)
stm.Type=2 'Read in this mode
stm.mode=3
stm.charset=utf-8
stm.open
stm.loadfromfile sfilename
f=stm.readtext
stm.Close
Set stm=Nothing
readfile=f
End Function
Regarding file encoding and web page encoding, please refer to the detailed explanation of the difference between character set Charset and file encoding Encoding.
Other sample programs
Copy the code code as follows:
'------------------------------------------------
'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 the 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
Among them, pay attention to the path problem in this line, stm.SaveToFile server.MapPath(FileUrl),2