Functions for generating static Web pages using ASP UTF-8 Encoding

Source: Internet
Author: User

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

Related Article

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.