1. #1
    Sencha Premium Member
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    330
    Vote Rating
    1
    PranKe01 is on a distinguished road

      0  

    Question [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:
    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;
        };
    I create the Base64 string like this:
    Code:
    var binary = c_FileManager.readFile('Stunden.xlsx');
    air.trace(encodeBase64(binary.toString()));
    The created string is not correct. It almost looks right, but some Bytes seem to be interpreted not correct.

    This is how it should look:
    Code:
    ...NtWUQbCGicTcUg7osIbOa0sctUvM9eevciQlJWq8JZSMUOUIxH11fJbOcBI15tMRU5kX+QErMcSoWx82D5y8KFUhE...
    This is what I get:
    Code:
    ...NtWUQbCGicTcUg7osIbDHLVLzPXnr3IkJSVqvCWUjFDlCMR9dXyWznASNebTEVOZF/kBKzHEqFsfNg+cuFUhE...
    I think the problem is how I read the files or how I use the Base64 encoder with the ByteArray type.

    I don't know how I can solve the problem

  2. #2
    Sencha Premium Member
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    330
    Vote Rating
    1
    PranKe01 is on a distinguished road

      0  

    Default


    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?

  3. #3
    Sencha Premium Member
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    330
    Vote Rating
    1
    PranKe01 is on a distinguished road

      0  

    Default


    Hi guys,

    I fixed the problem Here is the code I use to convert ByteArrays to Base64 and Base64 to ByteArrays:

    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;
    };
    It took me some days, to find the problem - I hope I can help somebody with this

  4. #4
    Sencha User
    Join Date
    Dec 2008
    Posts
    105
    Vote Rating
    0
    piroman is on a distinguished road

      0  

    Default


    LOOOOOOOL what a mess

    why dont u just use following functions
    atob : base64decode
    btoa : base64encode

    hope it helps

  5. #5
    Sencha Premium Member
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    330
    Vote Rating
    1
    PranKe01 is on a distinguished road

      0  

    Default


    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.

  6. #6
    Sencha User
    Join Date
    Nov 2010
    Posts
    4
    Vote Rating
    0
    nrako is on a distinguished road

      0  

    Thumbs up Thank you so much!!

    Thank you so much!!


    You saved my day thank you so much!

  7. #7
    Sencha User
    Join Date
    Mar 2007
    Posts
    580
    Vote Rating
    0
    dolittle is an unknown quantity at this point

      0  

    Default


    @PranKe01

    I'm using your code and it works great but I saw weird result recently.
    I'm trying to base64 encode the array
    Code:
    [131,104,5]
    which should result with:
    Code:
    "g2gF"
    Sometimes I'm getting:
    Code:
    "g2gf"
    lower f instead of capital F.

    I'm still not sure on what platform, OS, browser I'm getting the wrong lower f.

    Any idea how to fix it?

Thread Participants: 3

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi