Flash / Flex / ActionScript/Network/URLRequest

Материал из Web эксперт
Перейти к: навигация, поиск

Adding an asset to the display list

 
package {
  import flash.display.*;
  import flash.net.URLRequest;
  import flash.events.*;
  public class Main extends Sprite {
    private var loader:Loader;
    public function Main() {
      loader = new Loader();
      loader.contentLoaderInfo.addEventListener(Event.INIT, initListener);
      var urlRequest:URLRequest = new URLRequest("sunset.jpg");
      loader.load(urlRequest);
    }
    private function initListener (e:Event):void {
      addChild(loader.content);  // Add Bitmap object to SunsetViewer
    }
  }
}



Add URLrequest to Loader

 
package {
     import flash.display.Loader;
     import flash.display.Sprite;
     import flash.net.URLRequest;
     
     [SWF(width=550, height=400)]
     
     public class Main extends Sprite {
          
          public function Main() {
               var loader:Loader = new Loader();
               addChild(loader);
               loader.load(new URLRequest("audrey_computer.png"));
          }
          
     }
}



Formatting text with an externally loaded style sheet

 
package {
  import flash.display.*;
  import flash.text.*;
  import flash.events.*;
  import flash.net.*;
  public class Main extends Sprite {
    public function Main(  ) {
      var urlLoader:URLLoader = new URLLoader(  );
      urlLoader.addEventListener(Event.ruPLETE, completeListener);
      urlLoader.load(new URLRequest("styles.css"));
    }
    private function completeListener (e:Event):void {
        var styleSheet:StyleSheet = new StyleSheet(  );
        styleSheet.parseCSS(e.target.data);
        var t:TextField = new TextField(  );
        t.width = 200;
        t.styleSheet = styleSheet;
        t.htmlText = "<p>ActionScript is fun!</p>";
    }
  }
}



Loading a bitmap at runtime

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.net.*;
  public class Main extends Sprite {
    private var loader:Loader = new Loader(  );
    public function Main() {
      loader.contentLoaderInfo.addEventListener(Event.INIT,initListener);
      loader.load(new URLRequest("photo.jpg"));
    }
    private function initListener (e:Event):void {
      addChild(loader.content);
      trace(Bitmap(loader.content).bitmapData.getPixel(0, 0));
    }
  }
}



Loading XML Data: Event.COMPLETE listener

 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.net.*;
  public class XMLLoader extends Sprite {
    private var novel:XML;
    private var urlLoader:URLLoader;
    public function XMLLoader (  ) {
      var urlRequest:URLRequest = new URLRequest("novel.xml");
      urlLoader = new URLLoader(  );
      urlLoader.addEventListener(Event.ruPLETE, completeListener);
      urlLoader.load(urlRequest);
    }
    private function completeListener(e:Event):void {
      novel = new XML(urlLoader.data);
      trace(novel.toXMLString(  ));  
    }
  }
}



Loading XML Data from External Sources

 
package{
  import flash.display.Sprite;
  import flash.net.*;
  import flash.events.*;
  public class Main extends Sprite{
        var myXML:XML;
        var url:URLRequest = new URLRequest("http://www.your-domain.ru/test.xml");
        var loader:URLLoader = new URLLoader(url);
    public function Main(){
        loader.addEventListener(Event.ruPLETE, onLoadComplete);
    }
    function onLoadComplete(event:Event):void {
       if (loader.data) {
          myXML = XML(loader.data);
       }
    }
  }
}



Starting the load operation

 
package {
  import flash.display.*;
  import flash.net.URLRequest;
  public class Main extends Sprite {
    private var loader:Loader;
    public function Main(  ) {
      loader = new Loader(  );
      var url:URLRequest = new URLRequest("sunset.jpg");
        loader.load(url);
    }
  }
}



Use FileReference to download file from a URL

 
package{
  import flash.display.Sprite;
  import flash.net.*;
  public class Main extends Sprite{
    public function Main(){
        var fileReference:FileReference = new FileReference(  );
        
        var urlRequest:URLRequest = new URLRequest("http://www.myexamplesite.ru/example.txt");
        fileReference.download(urlRequest);
    }
  }
}