Use XMLDOM and ADODB. Stream to implement base64 encoding and decoding.

Source: Internet
Author: User

Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<Html>
<Head>
<Title> base64 encoding and decoding using XMLDOM and ADODB. Stream </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> base64 encoding and decoding using XMLDOM and ADODB. Stream <H5> By: CuiXiPing (unintentional)
<H4> 1. base64 encoding of the file
<Input type = file name = "file1" id = "file1" size = "50">
<Input type = "button" value = "base64 encoding" onclick = "if (! File1.value) {alert ('select A file')} else {textarea1.value = Base64EncodeFile (file1.value)} ">
<Br>
<Textarea id = "textarea1" rows = "6" cols = "80" readOnly> </textarea>

<H4> 2. base64 encoding of text
<Input type = "button" value = "base64 encoding" onclick = "textarea3.value = Base64EncodeText (textarea2.value)">
<Br>
<Textarea id = "textarea2" rows = "4" cols = "40"> resolutely resist Japanese goods </textarea>
<Textarea id = "textarea3" rows = "4" cols = "40" readOnly> </textarea>

<H4> 3. base64 Decoding of text
<Input type = "button" value = "base64 decoding" 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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.