开发者

When / how to initialize a URL request

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 fun开发者_如何学Goction 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.


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()">


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)" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜