Last active
November 7, 2025 07:08
-
-
Save nuintun/7508b9ddd8421e66f5e6 to your computer and use it in GitHub Desktop.
用 XMLDOM 和 ADODB.Stream 实现base64编码解码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Base64 | |
| * http://blog.csdn.net/cuixiping/article/details/409468 | |
| * ADODB.Stream 实例有 LoadFromFile 方法可以读取文件内容 | |
| * ADODB.Stream 实例有 state 属性 0 和 1,分别对应 Open 状态: adStateClosed 和 adStateOpen | |
| * ADODB.Stream 实例有 LineSeparator 属性 13、-1 和 10,分别对应换行符: adCR、adCRLF 和 adLF | |
| * ADODB.Stream 实例的其他属性和方法可参考相应文档 | |
| */ | |
| var Base64 = { | |
| /** | |
| * Base64 encode | |
| * | |
| * @param string | |
| * @returns {Base64} | |
| */ | |
| encode: function(string) { | |
| var base64; | |
| var xmldom = new ActiveXObject('MSXML2.DOMDocument'); | |
| var adostream = new ActiveXObject('ADODB.Stream'); | |
| var stream = xmldom.createElement('stream'); | |
| stream.dataType = 'bin.base64'; | |
| adostream.Charset = 'utf-8'; | |
| // 1=adTypeBinary 2=adTypeText | |
| adostream.Type = 2; | |
| adostream.Open(); | |
| adostream.WriteText(string); | |
| adostream.Position = 0; | |
| // 1=adTypeBinary 2=adTypeText | |
| adostream.Type = 1; | |
| // -1=adReadAll | |
| stream.nodeTypedValue = adostream.Read(-1); | |
| base64 = stream.text; | |
| adostream.Close(); | |
| // clean | |
| stream = null; | |
| adostream = null; | |
| xmldom = null; | |
| return base64; | |
| }, | |
| /** | |
| * Base64 decode | |
| * | |
| * @param base64 | |
| * @returns {String} | |
| */ | |
| decode: function(base64) { | |
| var string; | |
| var xmldom = new ActiveXObject('MSXML2.DOMDocument'); | |
| var adostream = new ActiveXObject('ADODB.Stream'); | |
| var stream = xmldom.createElement('stream'); | |
| stream.dataType = 'bin.base64'; | |
| stream.text = base64; | |
| adostream.Charset = 'utf-8'; | |
| // 1=adTypeBinary 2=adTypeText | |
| adostream.Type = 1; | |
| adostream.Open(); | |
| adostream.Write(stream.nodeTypedValue); | |
| adostream.Position = 0; | |
| // 1=adTypeBinary 2=adTypeText | |
| adostream.Type = 2; | |
| // -1=adReadAll | |
| string = adostream.ReadText(-1); | |
| adostream.Close(); | |
| // clean | |
| stream = null; | |
| adostream = null; | |
| xmldom = null; | |
| return string; | |
| } | |
| }; |
Author
nuintun
commented
Nov 7, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment