日期:2014-05-17  浏览次数:20770 次

ASP里如何生成 utf-8 的ASP文件
我最近在用ASP写一个生成网页的程序,要用到utf-8编码.
程序里有一个功能就是根据用户的选择去生成相应的ASP文件,

现在我用FSO去生成文件,但是如果包含中文的时候,用户调用这个文件,
中文就变成乱码.

在网上用找到用   adodb.stream   去实现的时候,发现所有的中文都不能写进文件里


------解决方案--------------------
fso.OpenTextFile( "c:\testfile.txt ", 2, True,0)

------解决方案--------------------
<%@language= "vbscript "%>
<%
Option Explicit
Response.Charset = "gb2312 "

Public Function getFileString(ByVal strPath, ByVal strCharset)
On Error Resume Next
Dim objFile
Set objFile = Server.CreateObject( "ADODB.Stream ")
objFile.Type = 2 'adTypeText
objFile.Open
objFile.Charset = strCharset
objFile.LoadFromFile strPath
getFileString = objFile.ReadText(-1)
objFile.Close
Set objFile = Nothing
If Err.Number Then Err.Clear
End Function

Public Function setFileString(ByVal strPath, ByVal strValue, ByVal strCharset, ByVal blnRewrite)
On Error Resume Next
Dim objFile
Set objFile = Server.CreateObject( "ADODB.Stream ")
objFile.Type = 2 'adTypeText
objFile.Open
objFile.Charset = strCharset
If blnRewrite = False Then
objFile.LoadFromFile strPath
objFile.Position = objFile.Size
End If
objFile.WriteText strValue
objFile.SaveToFile strPath, 2 'adSaveCreateOverWrite
objFile.Close
Set objFile = Nothing
setFileString = CBool(Err.Number = 0)
If Err.Number Then Err.Clear
End Function

setFileString Server.MapPath( "test.html "), " <html> " & _
" <head> " & _
" <meta http-equiv= " "content-type " " content= " "text/html; charset=utf-8 " " /> " & _
" <title> UTF-8测试 </title> " & _
" </head> " & _
" <body> " & _
" <h3> UTF-8中文测试 </h3> " & _
" </body> " & _
" </html> ", "utf-8 ", True

Response.Write " <a href= " "test.html " " target= " "_blank " "> 访问保存文件 </a> <br/> "
Response.Write "UTF-8 内容: "
Response.Write Server.HTMLEncode(getFileString(Server.MapPath( "test.html "), "utf-8 "))
%>