Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: decoding base64 data on client/browser?

From: "Joe Fawcett" <joefawcett@---------.------>
To: NULL
Date: 3/3/2006 8:44:00 AM
"Brandon" <Brandon@d...> wrote in message 
news:4FF749E8-874D-43AA-8143-456AFB5804D5@m......
> My browser app sends a request to the server for a binary file.  The 
> server
> receives the request, converts the file to base64, packages it up in XML, 
> and
> returns it to the client/browser.
>
> I can parse the returned XML just fine -- and find the base64 data -- but
> for the life of me cannot get it decoded back to it's original format. 
> The
> script below gets the filedata (using "nodeTypedValue") and saves it, but
> it's still the base64 representation of the file.
>
> I've tried a million different things, and have searched all over the web
> and newsgroups, and I just can't seem to find a combination that gets me 
> the
> original unencoded/decoded filedata.
>
> Two other notes:
> 1. On the server side, I set the dataType = "bin.base64"
> 2. When it's back here on the client, if I alert() out the dataType for 
> the
> node, it's null.  I thought that was interesting.
>
> Here is my client/browser JavaScript:
> var getdocs_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
> var getdocs_xmldoc  = new ActiveXObject("Msxml2.DOMDocument.4.0");
> getdocs_xmldoc.loadXML(reqxml);
> getdocs_xmlhttp.open("POST", "http://myserver/getbinfile.asp", false);
> getdocs_xmlhttp.send(getdocs_xmldoc);
>
>  getdocs_xmldoc.loadXML(getdocs_xmlhttp.responseText);
>  //getdocs_xmldoc.save(folderpath + "download.xml");
>  var root           = getdocs_xmldoc.documentElement;
>  var inodelist      = root.childNodes;
>
> ...
> parsing through for other data as well (i.e. filename)
> ...
>   if( nitem.nodeName == "filedata" )
>   {
>     var objStream = new ActiveXObject("ADODB.Stream")
>     objStream.Type = 1;
>     objStream.Open();
>     objStream.Write( nitem.childNodes.item(0).nodeTypedValue );
>     objStream.SaveToFile( folderpath + tfilename, (1 & 2) );
>     objStream.Close();
>   }
>
> ANY help would be MUCH appreciated.  Thank you all!
>
> -b
>
>
Not sure how you're creating the node which holds the binary data but it 
should have dt:dt="bin.base64" as an attribute.

Here are two scripts, the first saves a Word doc within a base64 node, the 
second recovers it:

//BinaryToXml.js
var WORD_PATH = "C:\\Scripts\\JS\\BinaryXml\\hello1.doc";
var XML_PATH = "C:\\Scripts\\JS\\BinaryXml\\word.xml"

function main()
{
  var oDoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
  oDoc.async = false;
  var oStream = new ActiveXObject("ADODB.Stream");
  oStream.type = 1; //Binary
  oStream.open();
  oStream.loadFromFile(WORD_PATH);
  var oNode = oDoc.createProcessingInstruction("xml", "version='1.0' 
encoding='utf-8'")
  oDoc.insertBefore(oNode, oDoc.childNodes[0]);
  var oRoot = oDoc.createElement("Root")
  oDoc.documentElement = oRoot;
  //Following line is not strictly necessary
  oRoot.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes");
  oNode = oRoot.appendChild(oDoc.createElement("wordDoc"));
  oNode.dataType = "bin.base64";
  oNode.nodeTypedValue = oStream.read();
  oStream.close();
  oDoc.save(XML_PATH);
  WScript.echo(oDoc.url);
}

main();


************************

//XmlToBinary.js
var WORD_PATH = "hello2.doc";
var XML_PATH = "word.xml"

function main()
{
  var oDoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
  oDoc.async = false;
  oDoc.load(XML_PATH);
  var oStream = new ActiveXObject("ADODB.Stream");
  oStream.type = 1; //Binary
  oStream.open();
  oStream.write(oDoc.documentElement.selectSingleNode("wordDoc").nodeTypedValue);
  oStream.savetoFile(WORD_PATH);
  oStream.close();
}

main();



-- 

Joe Fawcett -  XML MVP

https://mvp.support.microsoft.com/profile=8AA9D5F5-E1C2-44C7-BCE8-8741D22D17A5 




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent