Flash 如何使用AS3从web服务器加载二进制数据?

Flash 如何使用AS3从web服务器加载二进制数据?,flash,actionscript-3,Flash,Actionscript 3,我正试图像这样从服务器加载一些二进制数据 var urlRequest:URLRequest = new URLRequest("http://localhost/test.php"); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); loader.contentLoaderInfo.addEventListener(Sec

我正试图像这样从服务器加载一些二进制数据

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);
但它总是报告一个错误:

Loader Error #2124: Loaded file is an unknown type

我的二进制数据是自定义数据,但不是swf/image文件。flash不支持加载此类数据吗?或者我应该使用其他方法?

您考虑过实现套接字吗?这将使您能够更好地控制特定数据类型的二进制序列化

从Adobe文档中引用的代码示例:

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

当我必须将二进制数据(图像)从服务器端(PHP)发送到Flash(Flex)时,我通常使用64位(Base64编码器)对数据进行编码,然后使用Base64解码器类在Flex中对其进行*反*编码。希望这对您有所帮助。

您需要使用
urloader
而不是
Loader
。因为

Loader
对象仅用于加载SWF和图像。及


URLLoader
仅用于存储二进制数据或文本或二进制编码数据。

您需要使用URLLoader并指定请求的数据格式:

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

我已经得到了答案,应该使用URLLoad而不是加载器。因为引用二进制数据,您可能需要考虑套接字来进行二进制数据处理。很高兴您解决了URLLoader问题。我们使用http的原因只是想释放套接字服务器的沉重负载。还是谢谢你!