0

I have the following code. The problem is that neither SELECT nor CANCEL event never fired after i choose a file through browse dialog.

I have MacOS 10.6 and FlashPlayer 10.

What am i doing wrong?

package
{

  import flash.display.Sprite;
  import flash.display.StageAlign;
  import flash.display.StageScaleMode;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.net.FileReference;
  import flash.net.FileFilter;
  import flash.net.URLRequest;

    public class loader2 extends Sprite
    {
      private var placeholder:Sprite;
      private var fileReference:FileReference;
      private var fileFilter:FileFilter;

      public function loader2()
      {
        super();
        this.stage.align = StageAlign.TOP_LEFT;
        this.stage.scaleMode = StageScaleMode.NO_SCALE;
        this.stage.addEventListener(MouseEvent.CLICK, clickHandler);
        this.init();
      }

      protected function init():void
      {
        this.placeholder = new Sprite();
        this.placeholder.graphics.beginFill(0x999999);
        this.placeholder.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
        this.placeholder.graphics.endFill();

        this.placeholder.useHandCursor = true;
        this.placeholder.buttonMode = true;

        this.addChild(placeholder);
      }

      protected function getHostName():String
      {
        var url:String = this.loaderInfo.loaderURL;
        var protocolIndex:Number = url.indexOf("://");

        var index:Number = url.indexOf("/", protocolIndex + 3);

        return url.substr(0, index);
      }

      protected function getUploadUrl():String
      {
        var host:String = this.getHostName();
        var action:String = this.loaderInfo.parameters["uploadurl"];

        if (action.length == 0)
          return host;

        return host.concat(action);
      }

      protected function clickHandler(e:MouseEvent):void
      {
        this.fileFilter = new FileFilter( "Any file", "*.*" );
        this.fileReference = new FileReference();

        this.fileReference.addEventListener(Event.SELECT, selectHandler);
        this.fileReference.addEventListener(Event.COMPLETE, completeHandler);

        this.fileReference.browse([this.fileFilter]);
      }

      protected function selectHandler(e:MouseEvent):void
      {
      }

      protected function completeHandler(e:MouseEvent):void
      {
      }
    }
}
ILya
  • 2,670
  • 4
  • 27
  • 40

2 Answers2

2

The select and complete events are just a normal Event, not MouseEvent. Your handlers are expecting a MouseEvent, so you get an error when Flash tries to run them. Change the type of the event parameter in your handlers to Event:

protected function selectHandler(e:Event):void
// ...
protected function completeHandler(e:Event):void
Mike Welsh
  • 1,549
  • 1
  • 9
  • 14
  • looks like you beat me to it :) – Trevor Boyle Apr 04 '11 at 07:48
  • Great finding, Mike! I almost sure you are right. I wonder why i missed it. That's why copy-paste is evil))) I'll try to fix it this evening and then accept your answer. – ILya Apr 04 '11 at 09:00
1

Could it be as simple as choosing the wrong event type in the handlers signature eg. MouseEvent instead of Event ?

you may also like to add the following listener

this.fileReference.addEventListener(Event.CANCEL, cancelHandler);
Trevor Boyle
  • 1,025
  • 7
  • 15