The following fuctions wrapper the encoding/decoding functionality:

Private Function EncodeBase64(ByRef arrData() As Byte) As String

 

    Dim objXML As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement
   
   
' help from MSXML
    Set objXML = New MSXML2.DOMDocument
   
    ' byte array to base64
    Set objNode = objXML.createElement("b64")
    objNode.dataType = "bin.base64"
    objNode.nodeTypedValue = arrData
    EncodeBase64 = objNode.Text

 

    ' thanks, bye
    Set objNode =
Nothing
    Set
objXML = Nothing

 

End Function

 

Private Function DecodeBase64(ByVal strData As String) As Byte()

 

    Dim objXML As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement
   
   
' help from MSXML
   
Set objXML = New MSXML2.DOMDocument
    Set objNode = objXML.createElement("b64")
    objNode.dataType = "bin.base64"
    objNode.Text = strData
    DecodeBase64 = objNode.nodeTypedValue
   
    ' thanks, bye
    Set objNode =
Nothing
    Set
objXML = Nothing

 

End Function

 

以下内容转自互联网,版权归原作者所有

vbscript版本:

Function Base64Encode(psText)
dim oXml, oStream, oNode
Set oXml =Server.CreateObject("MSXML2.DOMDocument")
Set oStream =Server.CreateObject("ADODB.Stream")
Set oNode =oXml.CreateElement("tmpNode")
oNode.dataType ="bin.base64"
oStream.Charset ="gb2312"
oStream.Type =2'0=adStateClosed 1=adStateOpen
If oStream.state =0 Then oStream.Open()
oStream.WriteText(psText)
oStream.Position =0
oStream.Type =1'1=adTypeBinary   2=adTypeText
oNode.nodeTypedValue =oStream.Read(-1)'-1=adReadAll
oStream.Close()
Base64Encode =oNode.Text
Set oNode =Nothing
Set oStream =Nothing
Set oXml =Nothing
End Function


javascript版本:

function Base64EncodeText(TextStr){
var xml_dom =new ActiveXObject("MSXML2.DOMDocument");
var ado_stream =new ActiveXObject("ADODB.Stream");
var tmpNode =xml_dom.createElement("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
tmpNode.nodeTypedValue =ado_stream.Read(-1);//   -1=adReadAll
ado_stream.Close();
return tmpNode.text;
}


vbscript版本其实就是从javascript版本改过来的,可直接用于asp中,javascript版的也可以直接用于asp中,如果默认脚本语言是vbscript,则:

<script language="javascript" runat="server">

javascript版base64函数代码.....

</script>


另不止对字符串进行编码,还可以对文件进行编码,具体代码如下:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> 用 XMLDOM 和 ADODB.Stream 实现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" readOnly>
<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 = xml_dom.createElement("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);
tmpNode.nodeTypedValue = ado_stream.Read(-1); // -1=adReadAll
ado_stream.Close();

return tmpNode.text;
}
function Base64EncodeText(TextStr){
var xml_dom = new ActiveXObject("MSXML2.DOMDocument");
var ado_stream = new ActiveXObject("ADODB.Stream");
var tmpNode = xml_dom.createElement("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
tmpNode.nodeTypedValue = ado_stream.Read(-1); // -1=adReadAll
ado_stream.Close();

return tmpNode.text;
}
function Base64DecodeText(Base64Str){
var xml_dom = new ActiveXObject("MSXML2.DOMDocument");
var ado_stream = new ActiveXObject("ADODB.Stream");
var tmpNode = xml_dom.createElement("tmpNode");
tmpNode.dataType = "bin.base64";
tmpNode.text = Base64Str;

ado_stream.Charset = "gb2312";
ado_stream.Type = 1; // 1=adTypeBinary 2=adTypeText
ado_stream.Open();
ado_stream.Write(tmpNode.nodeTypedValue);
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>
 

posted on 2011-04-27 11:34  沉默的心  阅读(2200)  评论(0编辑  收藏  举报