Load media, datas file, or variables

Load a data file

While you want to load a data file, use URLLoader.

It's possible to define the data format that URLLoader will load. This class have three type constants : BINARY, TEXT, VARIABLES files

So, you have to define the URLLoader dataFormat property.

Let's load a text file :

package;
import flash.Lib; 
import flash.net.URLRequest; 
import flash.events.Event; 
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
      class Main
      {
      		public var loader:Loader;
      		public var data_loader:URLLoader;
      		static function main()
      		{
      			new Main();
      		}
      		public function new()
      		{      			 
      			exemple3();
      		} 
      		public function exemple3()
      		{
      		    data_loader = new URLLoader();
      		    var data_format = URLLoaderDataFormat.TEXT;
      		    var url = new URLRequest('fichiers/texte.txt');
      		    
      		    chargeur_de_donnee.dataFormat = data_format;
      		    chargeur_de_donnee.addEventListener(Event.COMPLETE, loadingIsOver);
      		    chargeur_de_donnee.load(url);
      		}
      		public function loadingIsOver(e:Event)
      		{
      		    trace(e.currentTarget.data); //Display content loaded
      		}
      }

The file contains the following text :

"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod [...]."

In this example we use URLLoader class, and we define the data format on which one we are going to work URLLoaderDataFormat.TEXT.

We define the file URI that we want to load, we add an event listener waiting for the end of the loading. Finnally we load the file with load() method.

At the end of the loading, the function loadingIsOver() is executed and we display the datas loaded with trace() method.

Result :

output

Alternative content

Get Adobe Flash player

(100 times)