用 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 size="50">
<input type="button" value="base64编码">
<br>
<textarea rows="6" cols="80" readOnly></textarea>
<h4>2.对文本的base64编码</h4>
<input type="button" value="base64编码">
<br>
<textarea rows="4" cols="40">坚决抵制日货</textarea>
<textarea rows="4" cols="40" readOnly></textarea>
<h4>3.对文本的base64解码</h4>
<input type="button" value="base64解码">
<br>
<textarea rows="4" cols="40">vOG+9rXW1sbI1bv1</textarea>
<textarea 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>
您可能感兴趣的文章: