3 Replies Latest reply on Oct 14, 2010 6:49 AM by mlavie

    Can't Load a Text File's Content into a TextArea

    mlavie Level 1

      I am trying to do a very simple task: load the text contained in a text file to a TextArea. The file in question indeed exists.

       

      Below is my code. I hope someone can point out why the TextArea remains empty.

       

      TIA,

       

      mlavie

       

      The code:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

       

      <mx:Script>
        <![CDATA[
         protected function myButton_clickHandler(event:MouseEvent):void
         {
          var file:File = new File(); 
          file.url = "file:///C:/myTextFile.txt";
          var fileStream:FileStream = new FileStream();
          fileStream.open(file, FileMode.READ);
          var fileText:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
          fileStream.close();
         
          myTextArea.text = fileText;
         }
        ]]>
      </mx:Script>

       

      <mx:Button x="116" y="193" label="Button" id="myButton" click="myButton_clickHandler(event)"/>
      <mx:TextArea x="85" y="25" width="251" height="109" id="myTextArea"/>

       

      </mx:Application>

        • 1. Re: Can't Load a Text File's Content into a TextArea
          aktell2007 Level 1

          This has been always working for me: Adapt it as it is strait of one of my script databases.

           

          creationComplete="loadTextOne(event), loadTextTwo(event);"

          <!-- -->
          <!-- *********************************************************************** -->
          <!--     IMPORTING TEXT MESSAGES.     -->
          <!-- *********************************************************************** -->
          <!-- -->
                    <mx:Script>
                         <![CDATA[
                              import flash.net.URLLoader;
                              import flash.net.URLRequest;
                              import mx.events.FlexEvent;
                         ]]>
                    </mx:Script>

                    <mx:Script>
                         <![CDATA[
                              private var loaderOne:URLLoader = new URLLoader();
                              
                              private function loadTextOne(e:FlexEvent):void {
                                   loaderOne.addEventListener(Event.COMPLETE, completeHandlerOne);
                                   loaderOne.load(new URLRequest("com/assets/textFilesGeneralViews/GalleryGeneralMain.txt"));
                              }
                              private function completeHandlerOne(e:Event):void{
                                   txtGeneralViewOne.text = e.target.data;
                              }
                         ]]>
                    </mx:Script>

                    <mx:Script>
                         <![CDATA[
                              private var loaderTwo:URLLoader = new URLLoader();
                    
                              private function loadTextTwo(e:FlexEvent):void {
                                   loaderTwo.addEventListener(Event.COMPLETE, completeHandlerTwo);
                                   loaderTwo.load(new URLRequest("com/assets/textFilesGeneralViews/GalleryGeneralSub.txt"));
                              }
                              private function completeHandlerTwo(e:Event):void{
                                   txtArtViewWelcome.text = e.target.data;
                                 }
                         ]]>
                    </mx:Script>

          <!-- TEXT LOADER ONE. id="txtGeneralViewOne" -->
          <!-- TEXT LOADER TWO. id="txtArtViewWelcome" -->

           

           

          regards aktell2007

          • 2. Re: Can't Load a Text File's Content into a TextArea
            rtalton Level 4

            It looks like you are trying to access the file system from a Flex application. This is not allowed in Flex. You should be getting some kind of error.

            • 3. Re: Can't Load a Text File's Content into a TextArea
              mlavie Level 1

              Thanks for the comments, but I solved it - my mistake was using "mx:Application" instead of "mx:WindowedApplication

              ".