28
Jul
07

Loading files using the URLLoader and URLVariables classes

Not sure if this is helpful to anybody, but thought I’d throw it out there. The following basic example loads some random variables from an external text file and displays the events which were dispatched in a DataGrid control, as well as the loaded name/value pairs.

Full code after the jump.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" verticalAlign="middle" backgroundColor="white" creationComplete="init()"> 

    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection; 

            [Bindable]
            private var VARIABLES_URL:String = "params.txt"; 

            [Bindable]
            private var arrColl:ArrayCollection; 

            [Bindable]
            private var paramColl:ArrayCollection; 

            private var urlReq:URLRequest;
            private var urlLdr:URLLoader; 

            private function init():void {
                /* Initialize the two ArrayCollections objects with empty arrays. */
                arrColl = new ArrayCollection();
                paramColl = new ArrayCollection(); 

                /* Initialize the URLRequest object with the URL to the file of name/value pairs. */
                urlReq = new URLRequest(VARIABLES_URL); 

                /* Initialize the URLLoader object, assign the various event listeners, and load the specified URLRequest object. */
                urlLdr = new URLLoader();
                urlLdr.addEventListener(Event.COMPLETE, doEvent);
                urlLdr.addEventListener(Event.OPEN, doEvent);
                urlLdr.addEventListener(HTTPStatusEvent.HTTP_STATUS, doEvent);
                urlLdr.addEventListener(IOErrorEvent.IO_ERROR, doEvent);
                urlLdr.addEventListener(ProgressEvent.PROGRESS, doEvent);
                urlLdr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doEvent);
                urlLdr.load(urlReq);
            } 

            private function doEvent(evt:Event):void {
                arrColl.addItem({type:evt.type, idx:arrColl.length+1, eventString:evt.toString()}); 

                switch (evt.type) {
                    case Event.COMPLETE:
                        /* If the load was successful, create a URLVariables object from the URLLoader.data property and populate the paramColl ArrayCollection object. */
                        var ldr:URLLoader = evt.currentTarget as URLLoader;
                        var vars:URLVariables = new URLVariables(ldr.data);
                        var key:String; 

                        for (key in vars) {
                            paramColl.addItem({key:key, value:vars[key]});
                        } 

                        params.visible = true;
                        break;
                }
            }
        ]]>
    </mx:Script> 

    <mx:VBox>
        <mx:Label text="Events:" />
        <mx:DataGrid id="events" dataProvider="{arrColl}" rowCount="5">
            <mx:columns>
                <mx:DataGridColumn dataField="idx" headerText="#" width="20" />
                <mx:DataGridColumn dataField="type" headerText="Type" showDataTips="true" dataTipField="eventString" />
            </mx:columns>
        </mx:DataGrid>
    </mx:VBox> 

    <mx:VBox>
        <mx:Label text="Parameters:" />
        <mx:DataGrid id="params" dataProvider="{paramColl}" rowCount="5" visible="false">
            <mx:columns>
                <mx:DataGridColumn dataField="key" headerText="Key" />
                <mx:DataGridColumn dataField="value" headerText="Value" />
            </mx:columns>
        </mx:DataGrid>
    </mx:VBox> 

</mx:Application>

2 Responses to “Loading files using the URLLoader and URLVariables classes”


  1. 1 Des Jul 31st, 2007 at 12:02 am

    This was helpful to me. I’m porting a small Java Swing based application to Flex which stored individual user information in .properties files (One for each user.) I’ve been looking for several days for informationhow to load and read text based properties in Flex. Thanks for posting

  2. 2 Harry Dec 2nd, 2008 at 5:54 am

    I’ve been searching for solution on unload the data loaded using URLLoader by clicking a checkbox. Any help will be appreciated. Thanks.

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




July 2007
M T W T F S S
    Aug »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Badge Farm

  • Firefox 2
  • Powered by Redoable 1.2
  • Feeds burnt by Feedburner
  • Feed