The following function uses the FSO object, where the file location is fso.asp. FSO Object File encoding attribute only three kinds, system default, Unicode,ascii, and no we want to utf-8, so the general Chinese system on the use of FSO object generated files are gb2312 page encoding format, can not generate UTF-8 encoding, therefore, English and other Latin language and Chinese can be normal display, but like Russian and other non-Latin language, the page will appear garbled.
Copy 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 replace the FSO object with the ADODB.stream object, because the stream class has LoadFromFile and SaveToFile methods, and has a critical attribute charset, which is not available with the FSO. The following functions are written using ADODB.stream to successfully generate UTF-8 Web page files.
Copy 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 the use of FSO procedures, as long as the function to modify, the function name does not change, it can be normal operation, more convenient.
If you use a template to generate files, you also need to use the template file UTF-8 encoding read in, otherwise, the background to publish the correct file encoding, but the template file read in the FSO GB2312 encoding, template page of the Russian language and other non-Latin, will appear garbled. The function is modified as follows:
The ReadFile function of the FSO originally used
Copy 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 the adopted ADODB.stream
Note that the function ReadFile (sfilename,charset) charset parameter charset is removed or retained according to actual needs.
Copy Code code as follows:
Function ReadFile (sFileName)
Dim 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
For file Encoding and Web page encoding, refer to the "Character set charset and file encoding encoding".
Other sample Programs
Copy 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 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, this line should pay attention to the path problem, STM. SaveToFile server. MapPath (FILEURL), 2