利用ADODB.Stream判断文件编码2008-03-14 15:49ANSI无格式定义
EFBB BF UTF-8
FFFE UTF-16/UCS-2, little endian
FEFF UTF-16/UCS-2, big endian
FFFE 0000 UTF-32/UCS-4, little endian
0000 FEFF UTF-32/UCS-4, big endian
function checkcode(path)
set ateobject("adodb.stream")
objstream.Type=1
objstream.open
objstream.Position=0
objstream.loadfromfile path
ad(2)
If AscB(MidB(bintou,1,1))=&HEF And AscB(MidB(bintou,2,1))=&HBB Then
checkcode="utf-8"
ElseIf AscB(MidB(bintou,1,1))=&HFF And AscB(MidB(bintou,2,1))=&HFE Then
checkcode="unicode"
Else
checkcode="gb2312"
End If
objstream.close
set objstream=nothing
end function
UTF8文件有BOM和No BOM两种,其区别在于BOM多了三个字节(EFBBBF),对比了二进制文件,发现有这个区别,如果是NoBOM的文件就无法通过前两个字节判断是否为UTF-8文件。
利用Adodb.Stream对象处理Charset2008-03-13 16:52Function tran_ado(ByVal strA As String, _
Optional ByVal strInCode As String = "gb2312", _
Optional ByVal strOutCode As String = "big5") As String
'本函数进行编码转换。
'对应的 strInCode 和 strOutCode 在注册表以下位置中能到
'HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MIME\Database\Charset
'请先引用 Microsoft ActiveX Data Objects 2.5 Library
url编码和utf8区别'或者以上版本
Dim Stm As New ADODB.Stream
'默认情况下是将简体中文转换为繁体中文
Stm.Type = adTypeText
Stm.Mode = adModeUnknown
Stm.Open
Stm.Charset = strOutCode
Stm.WriteText strA
Stm.Position = 0
Stm.Type = adTypeText
Stm.Charset = strInCode
tran_ado = Stm.ReadText()
Stm.Close
End Function
Function runTest()
Dim objFile, stmFile
Dim strText As String
Set objFile = CreateObject("Scripting.FileSystemObject")
Set stmFile = objFile.OpenTextFile("c:\", 1, False)
strText = stmFile.ReadAll
strText = tran_ado(strText, "gb2312", "utf-8") '出来的就是 UTF-8 了
stmFile.Close
Set stmFile = objFile.CreateTextFile("c:\", True)
stmFile.Write strText
stmFile.Close
End Function
生成utf-8编码的静态页,要用ADODB.Stream代替FSO读取文件,因为FSO不能生成utf-8编辑的页面2009-07-21 15:23生成utf-8编码的静态页,要用ADODB.Stream代替FSO读取文件,因为FSO不能生成utf-8编辑的页面。 程序代码<%@ CODEPAGE=65001 %>
<% Response.Charset="UTF-8" %>
<% Response.Buffer=True %>
<%
Filen = "template/test.htm"
Template = ReadFromUTF(Filen,"utf-8") '读取模板,注意编码对应模板文件编码
strtitle = "标题"
strcontent = "内容"
content = Replace(Template, "#title#",strtitle)
content = Replace(content, "#content#",strcontent)
sFilen = "html/
test.htm"
Call WriteToUTF(content,sFilen)'生成静态页,其中存放文件夹需要手动建立
If Not Err Then Response.Write "成功,请等待返回"
Response.Write "<META http-equiv='refresh' content='2;url=rehtm.asp'>"
Function ReadFromUTF(TempString,CharSet) 'TempString要读取的模板文件路径; Charset是编码
Dim str
Set stm = server.CreateObject("adodb.stream")
stm.Type = 2 'adTypeBinary=1 adTypeText=2
stm.Mode = 3 'adModeRead=1 adModeWrite=2 adModeReadWrite=3
stm.Charset = CharSet
stm.Open
stm.loadfromfile Server.MapPath(TempString)
str = adtext
stm.Close
Set stm = Nothing
ReadFromUTF = str
End Function
Function WriteToUTF(content,sFilen)'将替换后的内容写入HTML文档,content为替换后的字符串,filename为生成的文件名
Set objStream = Server.CreateObject("ADODB.Stream")
With objStream
.Open
.Charset = "utf-8"' //编码,这里你可以改成任何编码
.Position = objStream.Size
.WriteText = content' //模版+数据 写入内容
.SaveToFile server.mappath(sFilen),2' //生成文件路径
.Close
End With
Set objStream = Nothing
End Function
%>
模板页test.htm代码
程序代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>#title#</title>
</head>
<body>
#content#
</body>
</html>
asp生成utf-8格式静态文件的方法
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
'adodb读utf-8格式文本
function ReadFromUTF (TempString,CharSet) 'TempString要读取的模板文件路径; Charset是编码
dim str
set stm=server.CreateObject("adodb.stream")
stm.Type=2 'adTypeBinary=1 adTypeText=2
stm.charset=CharSet
stm.open
stm.loadfromfile server.MapPath(TempString)
adtext
stm.Close
set stm=nothing
ReadFromUTF=str
end function
dim TEMP
TEMP=ReadFromUTF("pdnavi2.asp","UTF-8") '读取举例
'写入UTF-8格式文件
Sub WriteToUTF(FileUrl,Str,CharSet) 'FileUrl是文件保存的文件名,Str是要写入的内容,CharSet是采用什么编码写入
set stm=server.CreateObject("adodb.stream")
stm.Type=2
stm.charset=CharSet
stm.open
stm.WriteText str
stm.SaveToFile server.MapPath(FileUrl),2
stm.flush
stm.Close
set stm=nothing
end Sub
Call WriteToUTF("pdnavi3.asp",TEMP,"UTF-8") '写入举例
%>
用 XMLDOM 和 ADODB.Stream 实现base64编码解码2009年11月27日 星期五 08:12vbs小铺
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> 用 XMLDOM 和 ADODB.Strea
m 实现base64编码解码 </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css" title="">
#textarea1,#textarea3,#textarea5 { background-color: #EEEEEE; }
</style>
</head>
<body>
<h3>用 XMLDOM 和 ADODB.Stream 实现base64编码解码</h3>
<h5>By: CuiXiPing(无心)</h5>
<h4>1.对文件的base64编码</h4>
<input type=file name="file1" id="file1" size="50">
<input type="button" value="base64编码" onclick="if(!file1.value){alert('请选择文件')}else{textarea1.value=Base64EncodeFile(file1.value)}">
<br>
<textarea id="textarea1" rows="6" cols="80" readOnly></textarea>
<h4>2.对文本的base64编码</h4>
<input type="button" value="base64编码" onclick="textarea3.value=Base64EncodeText(textarea2.value)">
<br>
<textarea id="textarea2" rows="4" cols="40">坚决抵制日货</textarea>
<textarea id="textarea3" rows="4" cols="40" readOnly></textarea>
<h4>3.对文本的base64解码</h4>
<input type="button" value="base64解码" onclick="textarea5.value=Base64DecodeText(textarea4.value)">
<br>
<textarea id="textarea4" rows="4" cols="40">vOG+9rXW1sbI1bv1</textarea>
<textarea id="textarea5" rows="4" cols="40" readOnly></textarea>
<script language="javaScript">
<!--
function Base64EncodeFile(fileSpec){
var xml_dom = new ActiveXObject("MSXML2.DOMDocument");
var ado_stream = new ActiveXObject("ADODB.Stream");
var tmpNode = ateElement("tmpNode");
tmpNode.dataType = "bin.base64";
ado_stream.Type = 1; // 1=adTypeBinary
if(ado_stream.state==0){ // 0=adStateClosed 1=adStateOpen
ado_stream.Open();
}
ado_stream.LoadFromFile(fileSpec);
ado_stream.Close();
;
}
function Base64EncodeText(TextStr){
var xml_dom = new ActiveXObject("MSXML2.DOMDocument");
var ado_stream = new ActiveXObject("ADODB.Stream");
var tmpNode = ateElement("tmpNode");
tmpNode.dataType = "bin.base64";
ado_stream.Charset = "gb2312";
ado_stream.Type = 2; // 1=adTypeBinary 2=adTypeText
if(ado_stream.state==0){ // 0=adStateClosed 1=adStateOpen
ado_stream.Open();
}
ado_stream.WriteText(TextStr);
ado_stream.Position = 0;
ado_stream.Type = 1; // 1=adTypeBinary 2=adTypeText
ado_stream.Close();
;
}
function Base64DecodeText(Base64Str){
var xml_dom = new ActiveXObject("MSXML2.DOMDocument");
var ado_stream = new ActiveXObject("ADODB.Stream");
var tmpNode = ateElement("tmpNode");
tmpNode.dataType = "bin.base64";
< = Base64Str;
ado_stream.Charset = "gb2312";
ado_stream.Type = 1; // 1=adTypeBinary 2=adTypeText
ado_stream.Open();
ado_stream.deTypedValue);
ado_stream.Position = 0;
ado_stream.Type = 2; // 1=adTypeBinary 2=adTypeText
var str = ado_stream.ReadText(-1); // -1=adReadAll
ado_stream.Close();
return str;
}
//-->
</script>
</body>
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论