-
18 Jan 2010 1:59 AM #1
[SOLVED] Reading any file type and encoding it to Base64
[SOLVED] Reading any file type and encoding it to Base64
Hi guys,
I spent days on this problem and no one seems to have a solution.
I want to communicate with a WebService via SOAP. This WebService got a function to send and receive files, which are encoded in Base64. I found a lot of implementations of the Base64 encoder/decoder - for example this one: http://www.webtoolkit.info/javascript-base64.html
Adobe Air uses WebKit so there are also the functions atob and btoa available.
The Base64 thing should work. Now the reading of the files... I want to read any type of file - I think this function should work:
I create the Base64 string like this:Code:this.readFile = function(inputfile){ var file = new air.File(inputfile); var bytes = new air.ByteArray(); if (file.exists) { var stream = new air.FileStream(); stream.open(file, air.FileMode.READ); bytes.endian = air.Endian.LITTLE_ENDIAN; stream.readBytes(bytes, 0, file.size); stream.close(); } return bytes; };
The created string is not correct. It almost looks right, but some Bytes seem to be interpreted not correct.Code:var binary = c_FileManager.readFile('Stunden.xlsx'); air.trace(encodeBase64(binary.toString()));
This is how it should look:
This is what I get:Code:...NtWUQbCGicTcUg7osIbOa0sctUvM9eevciQlJWq8JZSMUOUIxH11fJbOcBI15tMRU5kX+QErMcSoWx82D5y8KFUhE...
I think the problem is how I read the files or how I use the Base64 encoder with the ByteArray type.Code:...NtWUQbCGicTcUg7osIbDHLVLzPXnr3IkJSVqvCWUjFDlCMR9dXyWznASNebTEVOZF/kBKzHEqFsfNg+cuFUhE...
I don't know how I can solve the problem
-
22 Jan 2010 9:02 AM #2
I think the problem is when I cponvert the ByteArray to the Base64 string. When I read in a file and write it back, it works. But when I read a file and convert it to Base64 some pieces of the string are wrong. Maybe some bytes are not supported to convert?
Any hints?
-
25 Jan 2010 6:25 AM #3
Hi guys,
I fixed the problem
Here is the code I use to convert ByteArrays to Base64 and Base64 to ByteArrays:
It took me some days, to find the problem - I hope I can help somebody with thisCode: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; };
-
26 Jan 2010 7:24 AM #4
LOOOOOOOL what a mess
why dont u just use following functions
atob : base64decode
btoa : base64encode
hope it helps
-
26 Jan 2010 7:34 AM #5
No it doesn't. Thanks for your answer - next time please read the whole thread insteas of only the last one.
The WebKit functions does NOT support ByteArrays! My function does only work with them.
-
7 Mar 2011 5:17 AM #6
Thank you so much!!
Thank you so much!!
You saved my day thank you so much!
-
22 Aug 2011 11:18 AM #7
@PranKe01
I'm using your code and it works great but I saw weird result recently.
I'm trying to base64 encode the array
which should result with:Code:[131,104,5]
Sometimes I'm getting:Code:"g2gF"
lower f instead of capital F.Code:"g2gf"
I'm still not sure on what platform, OS, browser I'm getting the wrong lower f.
Any idea how to fix it?


Reply With Quote