• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

Reading text files

New Here ,
Dec 07, 2010 Dec 07, 2010

Copy link to clipboard

Copied

I am having trouble figuring out something that is very common in other languages: how to read a local text file. Can somebody enlighten me on how to do this in Flex 4? Thanks!

Views

2.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 07, 2010 Dec 07, 2010

Copy link to clipboard

Copied

There are a number of ways to achieve this. Can you be more specific as to what you mean by "local"

Is the file local to the file system that the SWF is running (i.e. on the server)  or is the file a local file on the end-user's machine that they have to select with a File Browser?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 08, 2010 Dec 08, 2010

Copy link to clipboard

Copied

By "local file system" I am referring to a text file that resides in the same directory on the server as the swf. I simply want to apply some configuraiton settings to my application. Thanks...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 08, 2010 Dec 08, 2010

Copy link to clipboard

Copied

One way to do it would be to use the URLLoader class:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html

http://actionscriptexamples.com/2008/02/27/loading-url-encoded-variables-into-a-flash-application-us...

//params.txt is a local file that includes: firstName=Tom&lastName=Jones
var lbl:TextField = new TextField();
var urlRequest:URLRequest = new URLRequest("params.txt");
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(urlRequest);
 
function urlLoader_complete(evt:Event):void {
    lbl.text = urlLoader.data.lastName + "," + urlLoader.data.firstName;
    addChild(lbl);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 08, 2010 Dec 08, 2010

Copy link to clipboard

Copied

LATEST

Or my personal favourite is to create a proper class and event to handle the loading of XML content...

[XMLContentLoader.as]

package

{

     import flash.events.Event;

     import flash.events.HTTPStatusEvent;

     import flash.events.IOErrorEvent;

     import flash.events.SecurityErrorEvent;

     import flash.net.URLLoader;

     import flash.net.URLLoaderDataFormat;

     import flash.net.URLRequest;

     import flash.net.URLRequestMethod;

     import flash.xml.XMLDocument;

    

     import mx.controls.Alert;

     import mx.rpc.xml.SimpleXMLDecoder;

         

     [Event(name="loadContent", type="XMLContentLoaderEvent")]

     [Event(name="contentLoaded", type="XMLContentLoaderEvent")]

     [Event(name="loadContentError", type="XMLContentLoaderEvent")]

    

     /**

     *

     * An XML Content Loader, that can load xml file and parse it to an

         * Object which will be dispatched with the Content Loaded event.

     *

     */

     public class XMLContentLoader extends URLLoader

     {

                   

          /**

          *

          * XML file url

          */

          public var url:String="";

               /**            *            * XML Content Loader state variable            */           public var state:int;

                                       

         

          /**

          * @Constructor

          */

          public function XMLContentLoader()

          {

               this.dataFormat = URLLoaderDataFormat.TEXT;

               super();

          }         

         

          /**

          *

          * Start to load content

          */

          public function sendRequest():void

          {

               try

               {

                    var _request:URLRequest = new URLRequest();    

                    _request.url = this.url;    

                    _request.method = URLRequestMethod.GET;

              

                    this.addEventListener(Event.COMPLETE, onResultHandler);

                    this.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);

                    this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);

                    this.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

                   

                    this.load(_request);

                    var ev:XMLContentLoaderEvent = new XMLContentLoaderEvent(XMLContentLoaderEvent.LOAD_CONTENT);                   

                    dispatchEvent(ev);

               }

               catch(e:Error)

               {

                    trace("Unable to load URL: "+this.url);

                    Alert.show("Unable to load URL: "+this.url, "XML Load Error");

               }

          }

         

          /**

          * On content loaded handler.

           *

          */

          private function onResultHandler(event:Event):void

          {

               var ev:XMLContentLoaderEvent = new XMLContentLoaderEvent(XMLContentLoaderEvent.CONTENT_LOADED);

               ev.data = convertXMLtoArrayCollection(new XMLDocument(event.target.data));

               ev.rawEvent = event;

               dispatchEvent(ev);

          }

         

          private function httpStatusHandler (e:HTTPStatusEvent):void

          {    

               this.state = e.status;

          }

         

          private function securityErrorHandler (e:SecurityErrorEvent):void

          {    

               trace("securityErrorHandler:" + e);

               Alert.show("securityErrorHandler:" + e, "XML Load Error");

          }

         

          private function ioErrorHandler(e:IOErrorEvent):void

          {    

              

               var ev:XMLContentLoaderEvent = new XMLContentLoaderEvent(XMLContentLoaderEvent.LOAD_CONTENT_ERROR);

               ev.rawEvent = e;

               dispatchEvent(ev);

               trace("ioErrorHandler: " + e);

               Alert.show("ioErrorHandler: " + e, "XML Load Error");

          }

         

          private function convertXMLtoArrayCollection(xmlDoc:XMLDocument):Object

          {

               var decoder:SimpleXMLDecoder = new SimpleXMLDecoder;

               var data:Object = decoder.decodeXML(xmlDoc);

              

               return data;

          }

         

         

     }

}

[XMLContentLoaderEvent.as]

package

{

     import flash.events.Event;

    

     public class XMLContentLoaderEvent extends Event

     {

         

          /**

          *

          * The xml file content will be converted and stored into this object.

          */

          public var data:Object;

         

          /**

          *

          * All original event info is saved in this variable.

          */

          public var rawEvent:Event;

         

          /**

          *

          * @default

          */

          public static const LOAD_CONTENT:String = "loadContent";

         

          /**

          *

          * @default

          */

          public static const CONTENT_LOADED:String = "contentLoaded";

         

          /**

          *

          * @default

          */

          public static const LOAD_CONTENT_ERROR:String = "loadContentError";

         

         

          /**

          *

          * @param type

          * @param bubbles

          * @param cancelable

          */

          public function XMLContentLoaderEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)

          {

               super(type, bubbles, cancelable);

          }

         

          /**

          *  @private

          */

          override public function clone():Event

          {

               var event:XMLContentLoaderEvent = new XMLContentLoaderEvent(type, bubbles, cancelable);

               event.data = data;

               event.rawEvent = rawEvent;              

               return event;                        

          }

     }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines