2009年11月27日 星期五

用 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>

註: 本文轉載自網路 非原創
轉載自 teN.potgnayiaH.wwW vbs小鋪 原文