开发者

Flex Base64decoding problem

I am 开发者_开发问答decoding a base64string I need to show the decoded contents in a window. But when i print that i am getting only the bytearrayObject and not the data.

How to get the data?

    private function copyByteArray(content:String):void{

        try{

            byteData = new ByteArray();
            //byteData.writeUTFBytes(contents);
            var dec:Base64Decoder = new Base64Decoder();
            dec.decode(content);

           byteData.writeBytes(dec.toByteArray());


            Alert.show("byte Array   " + byteData+" ::  " +contents.length + "::");
}
catch (ex: ErrorEvent){
        Alert.show("error");

    }

}


I'm pretty sure it will implicitly call toString for you when appending it to a string value unfortunately the toStrings tend to be pretty worthless.

You probably need to just step through the array and read in the data, do something like the following:

byteData.position=0;
var tempString:String = byteData.readUTFBytes(byteData.bytesAvailable);
Alert.show(tempString);

for other options for reading data from the ByteArray see the reference here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/ByteArray.html

Please validate that you reset the position of the byte array to 0, this appears to work for me (encoded "test" then decoded and shows correctly in the pop-up, not sure what else is expected?):

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                layout="vertical"
                minWidth="955"
                minHeight="600">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.utils.Base64Decoder;

            protected function button2_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                copyByteArray("dGVzdA==");

            }
            private function copyByteArray(content:String):void{

                try{

                    var byteData:ByteArray = new ByteArray();
                    //byteData.writeUTFBytes(contents);
                    var dec:Base64Decoder = new Base64Decoder();
                    dec.decode(content);

                    byteData.writeBytes(dec.toByteArray());
                    byteData.position=0;

                    Alert.show("byte Array   " + byteData.readUTFBytes(byteData.bytesAvailable)+" ::  " +content.length + "::");
                }
                catch (ex: ErrorEvent){
                    Alert.show("error");

                }
            }

        ]]>
    </mx:Script>
    <mx:Button label="Test Byte Array" click="button2_clickHandler(event)"/>
</mx:Application>


I tried it and got a string in Alert, not byteArray object. By the way, you should use a variable of class Error (or inherited classes), not any Events.


Try something like this:

var bytes:ByteArray = new ByteArray();
var bDecoder : Base64Decoder = new Base64Decoder();
bDecoder.decode(urlModifiedString);
bytes = bDecoder.toByteArray() ;

bytes.position = 0;

var returnObj : * = bytes.readObject();

after posting i just saw someone else's readUTFBytes... if you want to serialize and deserialize any type of object... roll with the above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜