The following function uses the FSO object, and the file is located in Fso. asp. There are only three file encoding attributes of the FSO object. The system defaults to Unicode and ASCII, and we do not need UTF-8. Therefore, the files generated by using the FSO object on the Chinese system are generally in the gb2312 webpage encoding format, unable to generate UTF-8 encoding, therefore, English and other Latin and Chinese can be normally displayed, but non-Latin like Russian, the page will appear garbled.
CopyCode The Code is 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
We chose to replace the FSO object with the ADODB. Stream object, because the stream class has the loadfromfile and savetofile methods, and has a crucial attribute charset, which is not available in Fso. The following functions are written in ADODB. Stream to successfully generate a UTF-8 web page file.Copy codeThe Code is 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
Set objstream = nothing
End Function
For FSOProgramYou only need to modify the function and the function name remains unchanged. This function can run normally, which is convenient and convenient.
If you use the template to generate a file, you also need to read the template file with UTF-8 encoding, otherwise, the background published the correct file encoding, but the template file read in is FSO gb2312 encoding, non-Latin lines such as Russian on the template page will be garbled. The function is modified as follows:
The original FSO readfile FunctionCopy codeThe Code is 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 the charset parameter of function readfile (sfilename, charset) is removed or retained as needed.Copy codeThe Code is 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 details about file encoding and webpage encoding, see "differences between Character Set charset and file encoding ".
other programs copy Code the code is as follows: '-------------------------------------------
'function name: readtextfile
': Use ADODB. stream object to read UTF-8 format text files
'----------------------------------------------------
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
'Purpose: 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
Note the path issue for this line. stm. savetofile server. mappath (fileurl), 2