2 Replies Latest reply on Apr 15, 2009 12:16 AM by theduk3

    Air - Download Image

      Hi there.

       

      I can't really find a proper example so I'm just asking here.

       

      I want to download an image and save it to a file.

       

      I've come this far:

       

      var request:URLRequest = new URLRequest("http://lh6.ggpht.com/_kjE-bzsNhyo/SdahYhoJbBI/AAAAAAAAQqU/kjYjHmo_DHA/s912/IMG_3533.JPG");
                  var loader:URLLoader = new URLLoader();
                  loader.dataFormat = URLLoaderDataFormat.BINARY;
                  loader.addEventListener(Event.COMPLETE, completeHandler);
                  try
                  {
                      loader.load(request);
                  }

      private function completeHandler(event:Event):void
              {
                  var arr:ByteArray = new ByteArray();
                  arr.writeObject(event.target.data);

                  trace(arr.length); 
                  write(arr);
              }
             
              public static function write(arr:ByteArray):void
              {
                var file:File = File.desktopDirectory.resolvePath("test.jpg");
                var fileStream:FileStream = new FileStream();
                fileStream.open(file, FileMode.WRITE);
                fileStream.writeBytes(arr);
                fileStream.close();
              }

       

      This works, I get the data and it has the appropriate length.

      The file is there, but obviously not properly serialized or whatever.

       

      So.. what else do I need to do?

       

       

       

       

      theduke