Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 27th, 2004, 01:42 AM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mookashi
Default Decoding Base64 Encoded Data and saving the image

hi,
im a new member to this forum. i am having a problem displaying the image passed over through an XML document.

my VB application receives an xml document in which one of the elements has base64encoded data of tiff image. Now i want to read this data and save the image in the local disk. i understand that we have to decode the data and then write into the disk in binary mode and all that. i have tried many options but nothing works. whenever i try to open the image that i have saved it says invalid data format. here's what im trying to do:

       Dim objFile as scripting.FileSystemObject
       Dim objImageFile as scripting.TextStream
       Dim btArr() as Byte

        strBody = oNode.Text
 ' This node contains the base64 encoded data
' I have even tried strBody=oNode.nodeTypedValue


        strBody = Replace(strBody, "\r\n", "")
        strBody = Replace(strBody, " ", "")
        strBody = Replace(strBody, vbCrLf, "")
        strBody = Replace(strBody, vbTab, "")

        strBody = CStr(Base64decode(strBody))
        btArr = strBody
' can i directly assign a string to a byte array as done above

       Set objImageFile = objFile.CreateTextFile("C:\test.tif", True)
       objImageFile.Write (btArr)
       objImageFile.Close

where im i going wrong?????
 
Old February 27th, 2004, 05:30 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Here are two JavaScripts I wrote to copy binary files to an xml doc and then extract and save to a new file. To test them out create a file, mine was a Word doc called 'hello1.doc' and save to the same folder as the two script files below. Run 'BinaryToXml.js' and examine the 'word.xml' file created. Then run 'XmlToBinary.js' to see the reverse. The main change to your code is to use the adodb.stream object which is better suited to binary files than the FileSystemObject.

Code:
//BinaryToXml.js
var WORD_PATH = "hello1.doc";
var XML_PATH = "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
  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);
}

main();
second file
Code:
//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();
If you have trouble converting to VB let me know.

--

Joe
 
Old February 27th, 2004, 07:59 AM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mookashi
Default

hi joe,
thanks for your reply. in fact i got this code from one of the previous posts that was available in this forum. i dont know if you had posted that one too. however i tried that code in a test vb application and it works for me.
i dont know why its not working for me in my main application. Probably i think in my main application im getting the xml file over the wire from a 3rd party so there might be some discrepancy over the way in the xml data is written.

Let me see if i can make sense to you. Now in the test application using the above code that u mentioned the values in Node.text and Node.nodeTypedvalue are different. Node.text contains base64encoded data while Node.nodetypedvalue contains a few unreadable characters(probably binary data)

but in my main application oNode.text and oNode.nodeTypedvalue contains the same value (i think its base64 encoded data) . So do you think the XML doc is not properly written from the other end?

Now again suppose if i get this base64encoded data in a string and pass the string to a decode function that i have which returns a string can i assign the returned string directly to a byte array and then write the byte array. i tried this and does not work.
any clarifications will be appreciated.

i would like to point out that the size of the file getting created is the same that is being created by test application i.e 39KB.but when i try to open it , it says invalid format. so i think its just some small thing that im missing somewhere!!

Also could you tell me whether Node.text (base64encoded data) should always be terminated by "==" (2 equal-to signs)
 
Old February 27th, 2004, 09:01 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Can you show how you retrieve the file etc? If you can show the url of your source I could test it from here.

The = sign at the end are padding to make the number of data characters a multiple of three. See http://email.about.com/cs/standards/...4_encoding.htm



--

Joe
 
Old February 28th, 2004, 03:07 AM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mookashi
Default

hi joe,
i finally managed to get the image displayed. what i was doing earlier was that i was decoding the encoded base64 data using a function and writing it to the disk. since that was not working, i just put the datatype value as "bin.base64" and then assigned the nodetypedvalue to my byte array and writing the bytearray.

previously i was not assigning the datatype value since i thought it had to be assigned from the other end.

well what matters in the end is that its working.
thanks for all ur help!!

 
Old September 21st, 2004, 11:55 AM
Registered User
 
Join Date: Sep 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by joefawcett
 Here are two JavaScripts I wrote to copy binary files to an xml doc and then extract and save to a new file. To test them out create a file, mine was a Word doc called 'hello1.doc' and save to the same folder as the two script files below. Run 'BinaryToXml.js' and examine the 'word.xml' file created. Then run 'XmlToBinary.js' to see the reverse. The main change to your code is to use the adodb.stream object which is better suited to binary files than the FileSystemObject.

Code:
//BinaryToXml.js
var WORD_PATH = "hello1.doc";
var XML_PATH = "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
  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);
}

main();
second file
Code:
//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();
If you have trouble converting to VB let me know.

--

Joe
 
Old September 21st, 2004, 11:57 AM
Registered User
 
Join Date: Sep 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe, I tried your javascript but I get an error at
"oNode.nodeTypeValue=ostream.Read();"
it gives me error message:
"object doesn't support this property or method"

please help
Eric

Quote:
quote:Originally posted by joefawcett
 Here are two JavaScripts I wrote to copy binary files to an xml doc and then extract and save to a new file. To test them out create a file, mine was a Word doc called 'hello1.doc' and save to the same folder as the two script files below. Run 'BinaryToXml.js' and examine the 'word.xml' file created. Then run 'XmlToBinary.js' to see the reverse. The main change to your code is to use the adodb.stream object which is better suited to binary files than the FileSystemObject.

Code:
//BinaryToXml.js
var WORD_PATH = "hello1.doc";
var XML_PATH = "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
  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);
}

main();
second file
Code:
//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();
If you have trouble converting to VB let me know.

--

Joe
 
Old September 21st, 2004, 12:09 PM
Registered User
 
Join Date: Sep 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

never mind, I found my problem:
nodeTypeValue -> nodeTypedValue.

thanks for the article.

Eric
Quote:
quote:Originally posted by datllc
 Hi Joe, I tried your javascript but I get an error at
"oNode.nodeTypeValue=ostream.Read();"
it gives me error message:
"object doesn't support this property or method"

please help
Eric

Quote:
quote:Originally posted by joefawcett
 Here are two JavaScripts I wrote to copy binary files to an xml doc and then extract and save to a new file. To test them out create a file, mine was a Word doc called 'hello1.doc' and save to the same folder as the two script files below. Run 'BinaryToXml.js' and examine the 'word.xml' file created. Then run 'XmlToBinary.js' to see the reverse. The main change to your code is to use the adodb.stream object which is better suited to binary files than the FileSystemObject.

Code:
//BinaryToXml.js
var WORD_PATH = "hello1.doc";
var XML_PATH = "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
  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);
}

main();
second file
Code:
//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();
If you have trouble converting to VB let me know.

--

Joe
 
Old September 21st, 2004, 02:18 PM
Registered User
 
Join Date: Sep 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe, I have a problem:
I'm trying to write a webservice that allows uploading of images. I used your javascript to create a soap message w/ the image. here my clientside code (it create a soap message) and it works:

        var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        function CreateXml()
        {
            var ostream=new ActiveXObject("ADODB.Stream");
            ostream.Type=1;
            alert(txtFile.value);
            ostream.Open();
            ostream.LoadFromFile(txtFile.value);
            var senv="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
            senv+="<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n";
            senv+="<soap:Body>\n";
                senv+="<upload xmlns=\"http://localhost/wsUpload\">\n";
                    senv+="<imageName>" + txtFile.value + "</imageName>\n";
                    senv+="<imgdata></imgdata>\n";
                senv+="</upload>\n";
            senv+="</soap:Body>\n";
            senv+="</soap:Envelope>\n";


            xmlhttp.Open("POST","http://localhost/wsUpload/UploadService.asmx",false);
            xmlhttp.setRequestHeader("Content-Type","text/xml");

This is my webservice method:

        [WebMethod(Description="upload images")]
        public void upload(String imageName,string imgdata)
        {
            //int Array
            try
            {
                String fileLoc=@"c:\temp\";
                FileStream fs=File.Create(fileLoc + "test.jpg");
                byte[] data=Convert.FromBase64String(imgdata);
                fs.Write(data,0,data.GetUpperBound(0) + 1);
                fs.Close();
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }


        }
            xmlhttp.setRequestHeader("SOAPAction","\"http://localhost/wsUpload/upload\"");

            var oDoc=new ActiveXObject("Microsoft.XMLDOM");
            oDoc.loadXML(senv);
            var oNode=oDoc.selectSingleNode("//imgdata");
            oNode.dataType="bin.base64";
            oNode.nodeTypedValue=ostream.Read();
            Textarea1.value=oDoc.xml;

            xmlhttp.Send(senv);
            ostream.Close();
        }


This is my webservice method:
        [WebMethod(Description="upload images")]
        public void upload(String imageName,string imgdata)
        {
            //int Array
            try
            {
                String fileLoc=@"c:\temp\";
                FileStream fs=File.Create(fileLoc + "test.jpg");
                byte[] data=Convert.FromBase64String(imgdata);
                fs.Write(data,0,data.GetUpperBound(0) + 1);
                fs.Close();
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }


        }

when I run it, the client side call the webservice method successfully, but only the imageName is passed to the method and NOT the imgdata.

Please help

Eric
Quote:
quote:Originally posted by joefawcett
 Here are two JavaScripts I wrote to copy binary files to an xml doc and then extract and save to a new file. To test them out create a file, mine was a Word doc called 'hello1.doc' and save to the same folder as the two script files below. Run 'BinaryToXml.js' and examine the 'word.xml' file created. Then run 'XmlToBinary.js' to see the reverse. The main change to your code is to use the adodb.stream object which is better suited to binary files than the FileSystemObject.

Code:
//BinaryToXml.js
var WORD_PATH = "hello1.doc";
var XML_PATH = "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
  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);
}

main();
second file
Code:
//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();
If you have trouble converting to VB let me know.

--

Joe
 
Old September 23rd, 2004, 11:33 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If the image data is showing in your textarea okay before sending I'm not sure what's wrong. I will try to reproduce but it will take a while to set up the pages etc.



--

Joe (Co-author Beginning XML, 3rd edition)





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Encode image to base64? OniShirox Javascript How-To 4 July 5th, 2007 03:10 AM
Image Editing and Saving aliirfan84 ASP.NET 2.0 Professional 2 May 28th, 2007 12:12 AM
Base64-encoded files harpua Classic ASP Professional 2 June 19th, 2006 12:04 PM
PNG image in base64 encoding janise PHP How-To 4 April 20th, 2006 02:30 PM
Image Upload to base64 format laker66 ASP.NET 1.0 and 1.1 Basics 1 April 8th, 2004 12:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.