Spark
Spark

Reputation: 672

How to use AS3 to load binary data from web server?

I'm trying to load some binary data from server like this

var urlRequest:URLRequest = new URLRequest("http://localhost/test.php");
var loader:Loader = new Loader();

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);   
loader.load(urlRequest);

But it always report an error:

Loader Error #2124: Loaded file is an unknown type

My binary data is custom data but not swf/image file. Does flash not support to load such data? Or I should use some other method?

Upvotes: 0

Views: 6987

Answers (4)

Cay
Cay

Reputation: 3794

You need to use URLLoader and specify the dataFormat of the request:

var request:URLRequest = new URLRequest(url);
var urlLoader:URLLoader = new URLLoader(request);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, onLoaded);


function onLoaded(e:Event):void {
  var bytes:ByteArray = urlLoader.data;
}

Upvotes: 5

Benny
Benny

Reputation: 2228

You needs to use URLLoader instead of Loader. Because

Loader object is useful for load swfs and images only . and

URLLoader only for store binary data or text or binary encoded data.

Upvotes: 1

Felipe
Felipe

Reputation: 11887

When I have to send binary data (images) from my server-side (PHP) to Flash (Flex) I usually encode the data in 64Bit (Base64 encoder) and then I *de*code it in Flex using the Base64Decoder class. Hope this helps you.

Upvotes: 0

Jason Sturges
Jason Sturges

Reputation: 15955

Have you thought about implementing sockets? This would give you greater control of binary serialization of specific data types.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/Socket.html

Code sample cited from Adobe documentation:

package {
    import flash.display.Sprite;

    public class SocketExample extends Sprite {
        private var socket:CustomSocket;

        public function SocketExample() {
            socket = new CustomSocket("localhost", 80);
        }
    }
}

import flash.errors.*;
import flash.events.*;
import flash.net.Socket;

class CustomSocket extends Socket {
    private var response:String;

    public function CustomSocket(host:String = null, port:uint = 0) {
        super();
        configureListeners();
        if (host && port)  {
            super.connect(host, port);
        }
    }

    private function configureListeners():void {
        addEventListener(Event.CLOSE, closeHandler);
        addEventListener(Event.CONNECT, connectHandler);
        addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
    }

    private function writeln(str:String):void {
        str += "\n";
        try {
            writeUTFBytes(str);
        }
        catch(e:IOError) {
            trace(e);
        }
    }

    private function sendRequest():void {
        trace("sendRequest");
        response = "";
        writeln("GET /");
        flush();
    }

    private function readResponse():void {
        var str:String = readUTFBytes(bytesAvailable);
        response += str;
    }

    private function closeHandler(event:Event):void {
        trace("closeHandler: " + event);
        trace(response.toString());
    }

    private function connectHandler(event:Event):void {
        trace("connectHandler: " + event);
        sendRequest();
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event);
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }

    private function socketDataHandler(event:ProgressEvent):void {
        trace("socketDataHandler: " + event);
        readResponse();
    }
}

Upvotes: 2

Related Questions