4 Replies Latest reply on Sep 1, 2010 5:45 AM by daleclarke

    How can I load a .txt file into my flex application?

    daleclarke

      I have a project where my client would like me to include a feature so he can easily update the text in this application by altering a .txt file.

      I have a scroll panel component and the code so far looks like this:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Group xmlns:s="library://ns.adobe.com/flex/spark" xmlns:fx="http://ns.adobe.com/mxml/2009">
          <s:RichText color="#2B4381" fontFamily="Arial" fontSize="108" height="1608" tabStops="S0 S50 S100 S150 S200 S250 S300" text="DYNAMIC TEXT TRIAL" width="292" x="0" y="0"/>
      </s:Group>

       

      Notice that this will always show "DYNAMIC TEXT TRIAL" and I cannot figure out how to load the .txt into it.

       

      Please help as this is a very important project!

       

      Thanks, Dale

        • 1. Re: How can I load a .txt file into my flex application?
          Subeesh Arakkan Level 4

          use HTTPService to load the text file.

           

          <?xml version="1.0" encoding="utf-8"?>
          <s:Group xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:fx="http://ns.adobe.com/mxml/2009"
               creationComplete="group1_creationCompleteHandler(event)">
               <fx:Declarations>
                    <s:HTTPService id="textService" 
                         resultFormat="text"
                         url="myText.txt"
                         result="textService_resultHandler(event)" fault="textService_faultHandler(event)"/>
               </fx:Declarations>
               <fx:Script>
                    <![CDATA[
                         import mx.events.FlexEvent;
                         import mx.rpc.events.FaultEvent;
                         import mx.rpc.events.ResultEvent;
                         
                         protected function textService_resultHandler(event:ResultEvent):void
                         {
                              richText.text = event.result as String;
                         }
                         
                         
                         protected function textService_faultHandler(event:FaultEvent):void
                         {
                              trace(event.fault.faultString);
                         }
                         
          
                         protected function group1_creationCompleteHandler(event:FlexEvent):void
                         {
                              textService.send();
                         }
          
                    ]]>
               </fx:Script>
               <s:RichText id="richText" color="#2B4381" fontFamily="Arial"  height="1608" fontSize="108"
                    text="DYNAMIC TEXT TRIAL" width="292" x="0" y="0"/>
          </s:Group>
          
          • 2. Re: How can I load a .txt file into my flex application?
            daleclarke Level 1

            Hi, thanks for the reply.

             

            Unfortuantely I cannot get it to work. I renamed my .txt file to myText.txt and kept used the same code but no luck...

            • 3. Re: How can I load a .txt file into my flex application?
              flex4 Level 2

              Hi

               

                              The previous code what he has posted was exactly correct.. change the name of text file in httpservice also and make sure the url is correct or not...

               

                        I am posting my example and this is in flex3

               

               

                                  <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
                 
                 
                  <mx:Script>
                      <![CDATA[
                          import mx.controls.Alert;
                          import mx.rpc.events.FaultEvent;
                          import mx.rpc.events.ResultEvent;
                     
                      public function handleResult(event:ResultEvent):void
                      {
                          myText.text = event.result as String;
                      }
                      public function handleFault(event:FaultEvent):void
                      {
                          Alert.show("some thing happend please check ur code");
                      }
                         
                      ]]>
                  </mx:Script>
                 
                  <mx:HTTPService id="ser" url="data/File.txt" result="handleResult(event)" fault="handleFault(event)" resultFormat="text"/>
                  <mx:TextArea id="myText" x="281" y="79" height="142"/>
                  <mx:Button label="Get The Text" click="ser.send()" x="319" y="229"/>  

               

              </mx:Application>

               

               

              and The File.txt file is

               

                             use ur own text file and have ur own stuff in the text file .. .. .

              1 person found this helpful
              • 4. Re: How can I load a .txt file into my flex application?
                daleclarke Level 1

                Thanks, I realised I posted the code in the wrong area.