Today, when I was correcting the utf-8 program in Search Bar, I found that the generated utf-8 format document had garbled code, and the original file was
The code of create_html.asp is as follows:
The code copy is as follows:
<%@LANGUAGE="VBSCRIPT"CODEPAGE="65001"%>
<%
setobjrs=server.createObject("Scripting.FileSystemObject")
conn=server.mappath("example.xml")
setStream=objrs.opentextfile(conn,1,true,-2)
content=stream.readall
Response.Write(content)
stream.close
%>
The function to implement this code is: read text including Chinese from example.xml (utf-8 format), and then output it, but every time the output is garbled. This problem really bothered me for a long time. Later, it was solved with the help of the classic forums "Xiao Han" and "Xiao Xiaoyu". Thank them.
Maybe I was wrong at the beginning, but now the correct code is modified as follows, using the code given by "Xiao Xiaoyu", including using the read content to generate a new utf-8 format document. The detailed code is as follows:
The code copy is as follows:
<%@LANGUAGE="VBSCRIPT"CODEPAGE="65001"%>
<%Response.CodePage=65001%>
<%Response.Charset="UTF-8"%>
<%
'Declare variables
dimread_path,write_paht,content
'----Read the file content--------------------------------------------------------------------------------------------------------------------
FunctionReadTextFile(filePath,CharSet)
dimstm
setstm=Server.CreateObject("adodb.stream")
stm.Type=1'adTypeBinary, read in according to binary data
stm.Mode=3'adModeReadWrite, only 3 can be used here to use others and will cause errors.
stm.Open
stm.LoadFromFilefilePath
stm.Position=0'Move the pointer back to the starting point
stm.Type=2'Text Data
stm.Charset=CharSet
ReadTextFile=stm.ReadText
stm.Close
setstm=nothing
EndFunction
'---Write to the file------------------------------
SubWriteTextFile(filePath,fileContent,CharSet)
dimstm
setstm=Server.CreateObject("adodb.stream")
stm.Type=2'adTypeText, text data
stm.Mode=3'adModeReadWrite, read and write, if this parameter uses 2, it will cause an error.