ASP reads UTF-8 documents and generates UTF-8 documents with garbled characters

Source: Internet
Author: User

I am searching for it today. Program During UTF-8 correction, garbled characters are found in the generated UTF-8 format document. The original file

Create_html.aspCodeAs follows:

Copy code The Code is 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 of this Code is as follows. XML (UTF-8 format) reads text including Chinese characters and then outputs, but every output is garbled. This problem has plagued me for a long time, later, I solved the problem with the help of the classic Forum "Xiaohan" and "Xiao Xiaoyu". thanks to them.

Maybe I was wrong at the beginning. Now the correct code is modified as follows, and the code "Xiao Xiaoyu" is used to generate a new UTF-8 format document using the read content. The Code is as follows:

Copy code The Code is as follows: <% @ Language = "VBScript" codePage = "65001" %>
<% Response. codePage = 65001%>
<% Response. charset = "UTF-8" %>
<%
'Declarative Variables
Dim read_path, write_paht, content
'---- Reading file content ------------------------
Function readtextfile (filepath, charset)
Dim STM
Set STM = server. Createobject ("ADODB. Stream ")
STM. type = 1' adtypebinary, read by binary data
STM. mode = 3'admodereadwrite. Here, only 3 can be used. Other errors will occur.
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 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 and write. If this parameter is set to 2, an error is returned.
STM. charset = charset
STM. Open
STM. writetext filecontent
STM. savetofile filepath, 2 'adsavecreateoverwrite, overwrite if the file exists
STM. Flush
STM. Close
Set STM = nothing
End sub
'File path to be read
Read_path = server. mappath ("example. xml ")
'File path to write
Write_path = server. mappath ("example. asp ")
'Read File Content
Content = readtextfile (read_path, "UTF-8 ")
'Output the file to be read
Response. Write (content)
'Start writing
Call writetextfile (write_path, content, "UTF-8 ")
%>

This code is quite practical and useful for generating static pages in UTF-8 format. I have also noted some necessary explanations. Note that:

Copy code The Code is as follows: <% @ Language = "VBScript" codePage = "65001" %>
<% Response. codePage = 65001%>
<% Response. charset = "UTF-8" %>

Do not forget the lines of code on your page. Otherwise, the output 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.