开发者

Flex: Read bytearray

开发者 https://www.devze.com 2023-01-17 13:33 出处:网络
I use the following to upload a file to Flex: private var filer:FileReference; protected function button1_clickHandler(event:MouseEvent):void

I use the following to upload a file to Flex:

        private var filer:FileReference;
        protected function button1_clickHandler(event:MouseEvent):void
        {
            var fd:String = "Files (*)"; 
            var fe:String = "*"; 
            var ff:FileFilter = new FileFilter(fd, fe);
            filer = new FileReference();
            filer.addEventListener(Event.SELECT, onFileSelect);
            filer.browse(new Array(ff));
            filer.addEventListener(Event.COMPLETE, 
                function (e:Event):void {
                    e.currentTarget.data.toString();
                }
            );
        }
        private function onFileSelect(e:Event):void {
            filer.load(); 
        }

And my file looks like this:

Flex: Read bytearray

Here is the original file: http://开发者_如何学Gosesija.com/up/1.txt

I need to read the uploaded file and parse it. The problem is that in my e.currentTarget.data.toString(); I get only '1' and not the rest of the String.

Any idea on how to successfully read this entire txt file?


The data property is a ByteArray. Instead of using the toString method (which apparently treats NULL byte as end of string), use specific read methods of the ByteArray class like readByte, readInt etc.

var array:Array = [];
var ba:ByteArray = e.currentTarget.data as ByteArray;
while(ba.bytesAvailable != 0){
    array.push(ba.readByte());
}
trace(array.join(", "));

You might want to read Working with byte arrays

0

精彩评论

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