0

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?

2
  • I already got the answer, should use URLLoader instead Loader. Commented Oct 19, 2011 at 4:21
  • Since you cited binary data, you might want to consider sockets for binary data handling. Glad you solved your URLLoader issue. Commented Oct 19, 2011 at 4:23

4 Answers 4

5

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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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();
    }
}

1 Comment

The reason why we use http is just want to release the heavy load from socket server. Thanks all the same!
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.