2 Replies Latest reply on Jan 25, 2010 11:40 AM by Pranke1

    Decode Base64 and save as binary file

    Pranke1

      Hi there,

       

      I am using Adobe Air 1.5 with JavaScript and want to save a file to my hard

      drive. I get the data from a WebService via SOAP as a Base64 encoded string. A test-string is attached. When I try to decode it with

      the WebKit function "atob()" and try to save this bytes with following code, I can't open the file.

       

      this.writeFile = function(outputfile, content, append){

      var file =a ir.File.applicationStorageDirectory.resolvePath(outputfile);

      var stream = newa ir.FileStream();

       

      if (append) {

      stream.open(filea, ir.FileMode.APPEND);

      }else {

      stream.open(filea, ir.FileMode.WRITE);

      }

       

      try{//Binärdaten

      stream.writeBytes(content0, , content.length);

      }catch(e){//Textdaten

      stream.writeUTFBytes(content);

      }

       

      stream.close();

      };

       

      The same happens when I try to open a file from my HDD and read in the bytes. When I decode it to base64, the string is not equal to the string, which is correct.

      I attached a working Base64 string, which I could convert back to a zip-file via a only encoder.

       

      So my question is, how can I decode a Base64 string and save the binary data to a file?

       

      Thank you for your help.

        • 1. Re: Decode Base64 and save as binary file
          Pranke1 Level 1

          No ideas whats going wrong? It is possible for me to read a file and write it directly from the ByteArray. But When I try to decode the ByteArray and then to encode, it fails

          • 2. Re: Decode Base64 and save as binary file
            Pranke1 Level 1

            I rewrote the Base64 decoder/encoder to use it with a ByteArray. Here ist the code:

             

            var byteArrayToBase64 = function(byteArr){
                var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
                 var encOut = "";
                var bits;
                var i = 0;
                 
                while(byteArr.length >= i+3){
                    bits = (byteArr[i++] & 0xff) << 16 | (byteArr[i++] & 0xff) << 8 | byteArr[i++] & 0xff;
                      encOut += base64s.charAt((bits & 0x00fc0000) >> 18) + base64s.charAt((bits & 0x0003f000) >> 12) + base64s.charAt((bits & 0x00000fc0) >> 6) + base64s.charAt((bits & 0x0000003f));
                }
                if(byteArr.length-i > 0 && byteArr.length-i < 3){
                    var dual = Boolean(byteArr.length - i - 1);
                    bits = ((byteArr[i++] & 0xff) << 16) | (dual ? (byteArr[i] & 0xff) << 8 : 0);
                    encOut += base64s.charAt((bits & 0x00fc0000) >> 18) + base64s.charAt((bits & 0x0003f000) >> 12) + (dual ? base64s.charAt((bits & 0x00000fc0) >> 6) : '=') + '=';
                }
                 
                return encOut;
            };
                 
            var base64ToByteArray = function(encStr){
                var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
                 var decOut = new air.ByteArray();  
                var bits;
                 
                for(var i = 0, j = 0; i<encStr.length; i += 4, j += 3){
                    bits = (base64s.indexOf(encStr.charAt(i)) & 0xff) <<18 | (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 | base64s.indexOf(encStr.charAt(i +3)) & 0xff;
                    decOut[j+0] = ((bits & 0xff0000) >> 16);
                      if(i+4 != encStr.length || encStr.charCodeAt(encStr.length - 2) != 61){
                           decOut[j+1] = ((bits & 0xff00) >> 8);
                      }
                      if(i+4 != encStr.length || encStr.charCodeAt(encStr.length - 1) != 61){
                           decOut[j+2] = (bits & 0xff);
                      }
                }
                
                return decOut;
            };