ASP reads Utf-8 format documents and generates garbled problems in Utf-8 format documents _ Application Tips

Source: Internet
Author: User
Today in the search for the program to Utf-8 correction, found that the generated Utf-8 format documents are garbled, the original file

The create_html.asp code is as follows:

Copy Code code as follows:

<% @LANGUAGE = "VBSCRIPT" codepage= "65001"%>
<%
Set Objrs=server.createobject ("Scripting.FileSystemObject")
Conn=server.mappath ("Example.xml")
Set Stream=objrs.opentextfile (conn,1,true,-2)
Content=stream.readall
Response.Write (content)
Stream.Close
%>

The function to implement this code is: read the text from Example.xml (utf-8 format) including Chinese, and then output, but each output is garbled, this problem really bothered me for a long time, and later still in the classic forum, "Xiao Han" "Rustling Rain" to solve the help, really thank them.

Maybe I was wrong in the beginning, and now the correct code is modified to use the "rustling" code, including the use of read content to generate new Utf-8 format documents. The detailed code is as follows:

Copy Code code as follows:

<% @LANGUAGE = "VBSCRIPT" codepage= "65001"%>
<% response.codepage=65001%>
<% response.charset= "UTF-8"%>
<%
' Declare the variable
Dim read_path,write_paht,content
'----Read the contents of the file------------------------
Function ReadTextFile (Filepath,charset)
Dim stm
Set Stm=server.createobject ("ADODB.stream")
Stm. Type=1 ' adTypeBinary, read in binary data
Stm. Mode=3 ' adModeReadWrite, there's only 3 to use.
Stm. Open
Stm. LoadFromFile FilePath
Stm. Position=0 ' Move the pointer back to the starting point
Stm. type=2 ' Text data
Stm. Charset=charset
ReadTextFile = stm. ReadText
Stm. Close
Set stm=nothing
End Function

'----Write to file------------------------
Sub WriteTextFile (Filepath,filecontent,charset)
Dim stm
Set Stm=server.createobject ("ADODB.stream")
Stm. type=2 ' adTypeText, text data
Stm. Mode=3 ' adModeReadWrite, read write, this parameter with 2 error
Stm. Charset=charset
Stm. Open
Stm. WRITETEXT filecontent
Stm. SaveToFile filepath,2 ' adsavecreateoverwrite, file exists to overwrite
Stm. Flush
Stm. Close
Set stm=nothing
End Sub
' The file path to read
Read_path = Server.MapPath ("Example.xml")
' The file path to write to
Write_path = Server.MapPath ("example.asp")
' Read the contents of the file
Content = ReadTextFile (Read_path, "Utf-8")
' Output read files
Response.Write (content)
' Start writing
Call WriteTextFile (write_path,content, "Utf-8")
%>


This code is quite practical, useful for generating static pages in utf-8 format, and some of the necessary explanations are noted:

Copy Code code as follows:

<% @LANGUAGE = "VBSCRIPT" codepage= "65001"%>
<% response.codepage=65001%>
<% response.charset= "UTF-8"%>
Your page do not forget these lines of code, or you read the output after the content is garbled.
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.