使用本地连接在 AIR 应用程序之间发送文件

发布于 2024-10-15 21:12:23 字数 2352 浏览 7 评论 0原文

如何使用本地连接在空中应用程序之间发送文本文件?

发件人 AIR

private var conn:LocalConnection;

            public function init():void
            {
                conn=new LocalConnection();
                conn.addEventListener(StatusEvent.STATUS,onStatus);             
            }

            private function Sender():void {

                var alphabets:File= File.createTempFile();
                var file:FileStream = new FileStream();
                file.open(alphabets,FileMode.WRITE);
                file.writeUTFBytes("Have a nice day");      
                file.close();
                conn.send("app#ReceiverAIR:MyConnection", "lcHandler",t1.text,alphabets);
            }

            private function onStatus(event:StatusEvent):void {
                switch (event.level) {
                    case "status":
                        trace("LocalConnection.send() succeeded");
                        break;
                    case "error":
                        trace("LocalConnection.send() failed");
                        break;
                }
            }           

    ]]>
</fx:Script>



<mx:TextArea id="t1" />
<mx:Button id="b1" label="Send" click="Sender()" />

接收器AIR

导入flash.net.LocalConnection;

        import mx.collections.ArrayCollection;

        private var conn:LocalConnection;

        public function LocalConnectionReceiverExample()     {

            conn = new LocalConnection();
            conn.client = this;
            try {
                conn.allowDomain('app#SenderAIR');
                conn.connect("MyConnection");

            } catch (error:ArgumentError) {
                trace("Can't connect...the connection name is already being used by another SWF");
            }
        }

        public function lcHandler(msg:String,myfile:File):void {
            trace("i am in lcHandler");
            t1.text=msg;    
        }

    ]]>
</fx:Script>

<mx:TextArea id="t1" editable="false"/>

它给出以下错误

Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: 
flash.net.LocalConnection was unable to invoke callback lcHandler. 
error=TypeError: Error #1034: 
Type Coercion failed: 
cannot convert Object@83d6791 to flash.filesystem.File.

How do i send a text file between air applications using local connection?

Sender AIR

private var conn:LocalConnection;

            public function init():void
            {
                conn=new LocalConnection();
                conn.addEventListener(StatusEvent.STATUS,onStatus);             
            }

            private function Sender():void {

                var alphabets:File= File.createTempFile();
                var file:FileStream = new FileStream();
                file.open(alphabets,FileMode.WRITE);
                file.writeUTFBytes("Have a nice day");      
                file.close();
                conn.send("app#ReceiverAIR:MyConnection", "lcHandler",t1.text,alphabets);
            }

            private function onStatus(event:StatusEvent):void {
                switch (event.level) {
                    case "status":
                        trace("LocalConnection.send() succeeded");
                        break;
                    case "error":
                        trace("LocalConnection.send() failed");
                        break;
                }
            }           

    ]]>
</fx:Script>



<mx:TextArea id="t1" />
<mx:Button id="b1" label="Send" click="Sender()" />

Receiver AIR

import flash.net.LocalConnection;

        import mx.collections.ArrayCollection;

        private var conn:LocalConnection;

        public function LocalConnectionReceiverExample()     {

            conn = new LocalConnection();
            conn.client = this;
            try {
                conn.allowDomain('app#SenderAIR');
                conn.connect("MyConnection");

            } catch (error:ArgumentError) {
                trace("Can't connect...the connection name is already being used by another SWF");
            }
        }

        public function lcHandler(msg:String,myfile:File):void {
            trace("i am in lcHandler");
            t1.text=msg;    
        }

    ]]>
</fx:Script>

<mx:TextArea id="t1" editable="false"/>

It gives the following error

Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: 
flash.net.LocalConnection was unable to invoke callback lcHandler. 
error=TypeError: Error #1034: 
Type Coercion failed: 
cannot convert Object@83d6791 to flash.filesystem.File.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-10-22 21:12:26

你不能用这种方式发送文件...我最终创建了一个字节数组并将字节数组发送到接收者。然后我将字节数组复制到接收器处的文件中。

u cant send a file this way... i finally created a byte array and sent the byte array to the receiver. Then i copied the byte array to a file at the receiver.

你又不是我 2024-10-22 21:12:24

进行编辑以反映OP的更改

感谢您的更新。据我了解,通过 LocalConnection 发送的所有对象都必须满足以下规定:

  1. 它们必须可使用 AMF 进行序列化
  2. 序列化数据不得超过 40K

我猜测 File 不是可序列化类型。

要尝试的事情:

  1. 注册一个别名。在两侧运行 flash.net.registerClassAlias('FileAlias', File)
  2. 发送更原始的数据类型。发送文件名还是文件数据?

祝你好运!
布莱恩

Edited to reflect changes from the OP

Thanks for the update. From what I understand, all the objects you send through a LocalConnection must meet the following stipulations:

  1. They must be serializable using AMF
  2. The serialized data must not exceed 40K

I am guessing that File is not a serializable type.

Things to try:

  1. Register an alias. Run flash.net.registerClassAlias('FileAlias', File) on both sides
  2. Send a more primitive data type. Send the file name or the file data instead?

Good luck!
Brian

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文