用XMLDOM和ADODB.Stream实现base64编码解码实现代码
<!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">
<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>