Wednesday, 16 March 2011

Flex4, Flex3, how to load an html to TextArea with htmlText property

problem: 
not something important... how to load a simple html file... or a plain text file

solution:
In this example I will create the "l" object where is a URLLoader.
The TextArea object is the "ta_plateu" object where is at "main" module on design time.
I have placed the htmlText: "Loading... please wait..."; this text will be appeared to the user till the download is completed and with this text I can discriminate if I have downloaded or not the text. (Of course this is not the perfect discrimination way, and it doesn't work well if the loaded text contains the "Loading..." text also).
use the follow code... accordingly:somewhere in your code you have to request the file...
      if (main.ta_plateu.htmlText.search("Loading...")>-1) {
        var l:URLLoader = new URLLoader;l.dataFormat=URLLoaderDataFormat.BINARY;
        l.load(new URLRequest("http://www.mycompany.com/docs/plateu_ital_text.html"));
        l.addEventListener(Event.COMPLETE,Plateu_download_completed);
      }
now you have to implement the listener "Plateu_download_completed" where will be triggered when the download is completed:
    public function Plateu_download_completed(event:Event):void{
      main.ta_plateu.htmlText=URLLoader(event.target).data;
    }

notes:
On this example are not checked the error conditions... you have to implement and the other events like I have done Event.COMPLETE.
Also the "l" object is not global in the class. Multiple call of the 1st block where you do the download request will have no so good performance. Its better to define the "l" object in the class (not in a function of your class) and remove the event listener in the "Plateu_download_completed" function.

No comments:

Post a Comment