用XMLDOM和ADODB.Stream實現base64編碼解碼實現代碼_javascript技巧

來源:互聯網
上載者:User
複製代碼 代碼如下:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.