Flash 何时/如何初始化URL请求

Flash 何时/如何初始化URL请求,flash,actionscript-3,flex4,flash-builder,mxml,Flash,Actionscript 3,Flex4,Flash Builder,Mxml,我做了一个图像上传管理器。我最初是在Flash中作为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="li

我做了一个图像上传管理器。我最初是在Flash中作为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\u btn”)和一个标签(id=“label\u txt”),用于显示各种状态消息


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

没有行号,我只能猜测,但可能
select\u btn
在此行为空:

select_btn.addEventListener( MouseEvent.CLICK, browse );
您可以将事件侦听器放在btn本身上:

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

可能
阶段
对象为空。在
声明中添加
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()">


按钮上有侦听器。很抱歉没有早点说。我正在考虑将swf加载到Flex中。这样行吗?正如我所说,swf工作得很好。@totbar我通常喜欢在尝试另一种方法之前找出失败的原因。哪一行有空对象?我更新了代码以显示mxml控件。根据错误消息,错误是第32行。@totbar如果我正确计算行数,第32行是
file=newfilereference()