何时/如何初始化URL请求

时间:2011-06-30 22:24:46

标签: flash actionscript-3 flex4 flash-builder mxml

我已经制作了一个图片上传管理器。我最初是在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 =“choices_btn”)和一个标签(id =“label_txt”),显示各种状态消息。

我尝试将init函数添加到组件的creationComplete事件中。我收到并且错误地说访问了一个空对象。

2 个答案:

答案 0 :(得分:1)

也许stage对象为空。在<s:Application>声明中添加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()">

答案 1 :(得分:0)

没有行号,我只能猜测,但此行可能select_btn为空:

select_btn.addEventListener( MouseEvent.CLICK, browse );

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

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