何时/如何初始化 URL 请求

发布于 2024-11-18 11:41:31 字数 3493 浏览 2 评论 0原文

我做了一个图像上传管理器。我最初是在 Flash Develop 中将其作为 AS 类制作的。我需要将其转换为 Flash Builder 4.5 中的组件。它作为 .swf 工作得非常好,但我不知道如何使 URL 请求在 Flash Builder 中工作。这是我在标签之间的内容:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
           creationComplete="init()">
<fx:Script>
    <![CDATA[
        import flash.display.MovieClip;
        import flash.display.*;
        import flash.events.*;
        import flash.text.*;

        import flash.net.FileReference;
        import flash.net.FileReferenceList;
        import flash.net.FileFilter;
        import flash.net.URLRequest;
        import flash.utils.Timer;
        import flash.events.TimerEvent;

        public var file:FileReference;
        public var filefilters:Array;
        public var req:URLRequest;
        public var tm:Timer;
        public var speed:Number = 0;
        public var currbytes:Number = 0;
        public var lastbytes:Number = 0;


        public function init():void{
            req = new URLRequest();
            req.url = ( stage.loaderInfo.parameters.f )? stage.loaderInfo.parameters.f : 'http://www.listgiant.com/LG/upload.php';
            file = new FileReference();
            setup( file );
            select_btn.addEventListener( MouseEvent.CLICK, browse );
            tm = new Timer( 1000 );
            tm.addEventListener( TimerEvent.TIMER, updateSpeed );
        }


        public function browse( e:MouseEvent ):void{
            filefilters = [ new FileFilter('Images', '*.jpg') ]; // add other file filters
            file.browse( filefilters );
        }

        private function setup( file:FileReference ):void{
            file.addEventListener( IOErrorEvent.IO_ERROR, io_error );
            file.addEventListener( Event.OPEN, open_func );
            file.addEventListener( Event.SELECT, selectHandler );
            file.addEventListener( DataEvent.UPLOAD_COMPLETE_DATA, show_message );      
        }
        private function io_error( e:IOErrorEvent ):void{
            label_txt.text = 'The file could not be uploaded.';
            tm.stop();
        }

        private function open_func( e:Event ):void{
            tm.start();
        }

        private function selectHandler( e:Event ):void{
            file.upload( req );

        }

        private function show_message( e:DataEvent ):void{
            tm.stop();
            if( e.data == 'ok' ){
                label_txt.text = 'The file has been uploaded.';
            } else if( e.data == 'error'){
                label_txt.text = 'The file could not be uploaded.';
            }
        }

        private function updateSpeed( e:TimerEvent ):void{
            speed = Math.round( (currbytes - lastbytes)/1024 );
            lastbytes = currbytes;
        }

        private function cancelUpload( e:MouseEvent ):void{
            file.cancel();
            reset();
        }

        private function reset():void{
            select_btn.visible = true;
            label_txt.text = '';
        }
]]>
</fx:Script>



<s:Button id="select_btn" label="Upload" click="browse(event)"/>
<s:Label id="label_txt" text=""/>

我没有放置 mxml 控件,但在显示各种状态消息的按钮下有一个浏览按钮 (id="selects_btn") 和一个标签 (id="label_txt")。

我尝试将 init 函数添加到组件的creationComplete 事件中。我收到错误消息说访问空对象。

I have made an image upload manager. I made it initially in Flash Develop as an AS class. I need to convert it to a component in Flash Builder 4.5 It works absolutely fine as a .swf, but I can't figure out how to make the URL request work in Flash Builder. This is what I have between the tags:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
           creationComplete="init()">
<fx:Script>
    <![CDATA[
        import flash.display.MovieClip;
        import flash.display.*;
        import flash.events.*;
        import flash.text.*;

        import flash.net.FileReference;
        import flash.net.FileReferenceList;
        import flash.net.FileFilter;
        import flash.net.URLRequest;
        import flash.utils.Timer;
        import flash.events.TimerEvent;

        public var file:FileReference;
        public var filefilters:Array;
        public var req:URLRequest;
        public var tm:Timer;
        public var speed:Number = 0;
        public var currbytes:Number = 0;
        public var lastbytes:Number = 0;


        public function init():void{
            req = new URLRequest();
            req.url = ( stage.loaderInfo.parameters.f )? stage.loaderInfo.parameters.f : 'http://www.listgiant.com/LG/upload.php';
            file = new FileReference();
            setup( file );
            select_btn.addEventListener( MouseEvent.CLICK, browse );
            tm = new Timer( 1000 );
            tm.addEventListener( TimerEvent.TIMER, updateSpeed );
        }


        public function browse( e:MouseEvent ):void{
            filefilters = [ new FileFilter('Images', '*.jpg') ]; // add other file filters
            file.browse( filefilters );
        }

        private function setup( file:FileReference ):void{
            file.addEventListener( IOErrorEvent.IO_ERROR, io_error );
            file.addEventListener( Event.OPEN, open_func );
            file.addEventListener( Event.SELECT, selectHandler );
            file.addEventListener( DataEvent.UPLOAD_COMPLETE_DATA, show_message );      
        }
        private function io_error( e:IOErrorEvent ):void{
            label_txt.text = 'The file could not be uploaded.';
            tm.stop();
        }

        private function open_func( e:Event ):void{
            tm.start();
        }

        private function selectHandler( e:Event ):void{
            file.upload( req );

        }

        private function show_message( e:DataEvent ):void{
            tm.stop();
            if( e.data == 'ok' ){
                label_txt.text = 'The file has been uploaded.';
            } else if( e.data == 'error'){
                label_txt.text = 'The file could not be uploaded.';
            }
        }

        private function updateSpeed( e:TimerEvent ):void{
            speed = Math.round( (currbytes - lastbytes)/1024 );
            lastbytes = currbytes;
        }

        private function cancelUpload( e:MouseEvent ):void{
            file.cancel();
            reset();
        }

        private function reset():void{
            select_btn.visible = true;
            label_txt.text = '';
        }
]]>
</fx:Script>



<s:Button id="select_btn" label="Upload" click="browse(event)"/>
<s:Label id="label_txt" text=""/>

I didn't put the mxml controls but there is a browse button (id="selects_btn") and a label (id="label_txt") under the button that displays various status messages.

I tried adding the init function to the component's creationComplete event. I receive and error saying access of a null object.

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

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

发布评论

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

评论(2

相权↑美人 2024-11-25 11:41:31

也许 stage 对象为 null。在 声明中添加 applicationComplete 属性并将值设置为 init() 方法,如下所示:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               applicationComplete="init()">

Maybe the stage object is null. In the <s:Application> declaration add the applicationComplete attribute and set the value to the init() method so it looks like the following:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               applicationComplete="init()">
蝶…霜飞 2024-11-25 11:41:31

没有行号,我只能猜测,但也许 select_btn 在这一行为空:

select_btn.addEventListener( MouseEvent.CLICK, browse );

您可以将事件侦听器放在 btn 本身上:

<s:Button id="select_bt" click="browse(event)" />

Without a line number, I can only guess, but maybe select_btn is null at this line:

select_btn.addEventListener( MouseEvent.CLICK, browse );

You could put the event listener on the btn itself:

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