ASP,ASP,UTF ASP模版类 UTF-8 编码出现乱码的问题

出处:http://www.it130.net/asp-asp.net/ASP-UTF-221300.htm
<%
' 这是模板对象。 它允许创建、读取
' 并且在服务器上编辑模板。
Class template

' 模板存放变量。
Private mytemplate 'as string

' 这个过程从服务器得到模板并返回
' 整体文件作为串。
Public Function gettemplate(pathfilename,encodeto html) 'as string
Dim corpFSO 'as object
Dim corpTemplate 'as object
Dim temptemplate 'as string

' 打开类型为模板(只读)
Const forreading = 1,boolcreatefile = False

If IsNull(encodeto html) Or encodeto html = "" Or encodeto html = False Then
encodeto html = False
Else
encodeto html = True
End If


On Error Resume Next
' 创建文件系统对象(FileSystemObject)
Set corpFSO = Server.CreateObject("Scripting.FilesyStemObject")

' 创建模版对象
Set corpTemplate = corpFSO.opentextfile(Server.MapPath(pathfilename),forreading,boolcreatefile)
If Err <> 0 Then
Err.Clear
Exit Function
End If
' 得到整体文件作为串
temptemplate = corpTemplate.readall

' 编码模板到 html
If encodeto html Then
gettemplate = to html(temptemplate)
Else
gettemplate = temptemplate
End If

' 关闭模板
corpTemplate.Cl ose

' 释放服务器资源
Set corpTemplate = Nothing

Set corpFSO = Nothing
End Function


' 这个过程得到并且存放模板
Public Sub usetemplate(pathfilename)
thistemplate = gettemplate(pathfilename,False)
End Sub


' 这Property用用户的模板替换标记
Public Property Let tag(tagname,userstring)
Dim ld, rd 'as string
Dim temptag 'as string
Dim tagstart, tagend 'as integer

ld = "<!--"
rd = "-->"
tagstart = 1

Do While tagstart > 0 And tagstart < Len(thistemplate)
tagstart = InStr(tagstart,thistemplate,ld, VBBinaryCompare)
If tagstart > 0 Then
tagend = InStr(tagstart,thistemplate,rd, VBBinaryCompare)
If tagend > (tagstart + 3) Then
temptag = Mid(thistemplate,tagstart + 4,tagend-(tagstart+4))
If (Trim(temptag) = tagname) Or (temptag = tagname) Then
If IsNull(userstring) Then
thistemplate = Replace(thistemplate,ld & temptag & rd,"")
Else
thistemplate = Replace(thistemplate,ld & temptag & rd,userstring)
End If
Exit Do
Else
tagstart = tagstart + 4
End If
End If
End If
Loop
End Property


Public Sub removeTags()
Dim ld, rd 'as string
Dim temptag 'as string
Dim tagstart, tagend 'as integer

ld = "<!--"
rd = "-->"
tagstart = 1

Do While tagstart > 0 And tagstart < Len(thistemplate)
tagstart = Instr(tagstart,thistemplate,ld, VBBinaryCompare)
If tagstart > 0 Then
tagend = Instr(tagstart,thistemplate,rd, VBBinaryCompare)
If tagend > (tagstart + 3) Then
temptag = Mid(thistemplate,tagstart + 4,tagend-(tagstart+4))
thistemplate = Replace(thistemplate,ld & temptag & rd,"")
tagstart = tagend
End If
End If
Loop
End Sub


' 这Property允许用户分配当前模板
Public Property Let thistemplate(template_as_string)
If VarType(template_as_string) = VBstring Or VarType(template_as_string) = VBnull Then
mytemplate = template_as_string
End If
End Property


' 这Property退回当前模板
Public Property Get thistemplate() 'as string
thistemplate = mytemplate
End Property


' 这个子程序显示当前模板
Public Sub display()
Response.Write thistemplate
End Sub

' 这个过程将模版内容作为 html输出
Public Sub htmlEncode()
to html(thistemplate)
End Sub


' 这个做法保存当前模板到服务器
Public Sub saveas(pathfilename)
Dim corpFSO 'as object
Dim corpTemplate,oBackup 'as object
Dim strTruePath 'as string

' 打开类型为模板(只读)
Const forreading = 1,forwriting = 2,boolcreatefile = True

On Error Resume Next

' 创建文件系统对象(FileSystemObject)
Set corpFSO = Server.CreateObject("Scripting.FilesyStemObject")

' 创建模板对象
strTruePath = Server.MapPath(pathfilename)
If corpFSO.fileexists(strTruePath) Then
corpFSO.copyfile strTruePath, strTruePath & ".bak", true
End If
Set corpTemplate = corpFSO.opentextfile(strTruePath,forwriting,boolcreatefile)
If Err <> 0 Then
Err.Clear
Exit Sub
End If

' 给服务器写整体模板
corpTemplate.Write thistemplate

' 关闭模板
corpTemplate.Cl ose

' 释放服务器资源
Set corpTemplate = Nothing

Set corpFSO = Nothing
End Sub


' 这个Function输入文本到 html
Private Function to html(temptemplate)
'temptemplate = Replace(temptemplate,"<","<")
temptemplate = Replace(temptemplate," "," ")
temptemplate = Replace(temptemplate, VBcrlf,"<br />")

to html = temptemplate
End Function

' 这个做法清除当前模板的变量
' 当对象被创造时,被存放在。
Private Sub class_initialize()
mytemplate = ""
End Sub


' 这个做法清除当前模板的可变物
' 当对象被终止时,被存放在。
Private Sub class_terminate()
Set mytemplate = Nothing
End Sub
End Class
%>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值