用 XMLDOM 和 ADODB.Stream 实现base64编码解码 (2005-07-01)
发布时间 2005-07-01 13:50:00
// 文件->base64
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.state0) { // 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;
}
// 文本->base64
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 = "utf-8";
ado_stream.Type = 2; // 1=adTypeBinary 2=adTypeText
if (ado_stream.state0) { // 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;
}
// base64->文本
function Base64DecodeText(Base64Str, charset) {
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 = charset || "utf-8";
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;
}