0

How to load a file from a user selection?

In later flex versions is available the method FileReference.load(). But in flex 2 there isn't.

I want to do something like this:

var fileRef:FileReference= new FileReference();
button.addEventListener(MouseEvent.CLICK, onButtonClick);

function onButtonClick(e:MouseEvent):void {
    fileRef.browse([new FileFilter("Images", "*.jpg;*.gif;*.png")]);
    fileRef.addEventListener(Event.SELECT, onFileSelected);
}

function onFileSelected(e:Event):void {
    fileRef.addEventListener(Event.COMPLETE, onFileLoaded);
    fileRef.load();
}

function onFileLoaded(e:Event):void {
    var loader:Loader = new Loader();
    loader.loadBytes(e.target.data);
}
Daniel Moura
  • 7,816
  • 5
  • 35
  • 60
  • Flex2 is using actionscript 3, not 2 (I think Flex 1.5 was on as2). Does this need to be compiled with the Flex2SDK ? Why not switch to FlexSDK 3 or newer (with Flash Player 10 support and FileReference's load()) ? Also, another option is to switch to AIR and use the flash.desktop.File API. – George Profenza Sep 18 '12 at 21:11
  • This code is for an addon for an application that only supports Flex2. I don't know about switching to AIR, I'll try to figure this out. – Daniel Moura Sep 18 '12 at 22:20
  • @GeorgeProfenza I can't use AIR for developing the addon. – Daniel Moura Sep 19 '12 at 12:46

0 Answers0