Search code examples
actionscript-3apache-flexexternalinterface

File dialog is not showing from flex function


I am trying to execute flex function from javascript with ExternalInterface and addCallback:

<s:Application 
  xmlns:fx="http://ns.adobe.com/mxml/2009" 
  xmlns:s="library://ns.adobe.com/flex/spark" 
  xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="initApp()">

import flash.external.*;
import flash.net.FileReference;

public function initApp():void {
  ExternalInterface.addCallback("sendTextFromJS", receiveTextFromJS);
}

public function receiveTextFromJS(s:String):void {
  l1.text = s;
  var myFileReference:FileReference = new FileReference();
  myFileReference.browse();
}

But for some reasons the file dialog is not showing, but the text from label with id l1 is changed.


Solution

  • the FileReference.browse action could be called only in response to a user action(mouse event or keypress event), so you have to modify your code to gain the user action, for example you can use Alert:

            public function receiveTextFromJS(s:String):void {
                Alert.show("Browse for files?", "", Alert.OK | Alert.CANCEL, null, onAlert);
            }   
    
            private function onAlert(event:CloseEvent):void
            {
                if(event.detail == Alert.OK)
                {
                    var myFileReference:FileReference = new FileReference();
                    myFileReference.browse();
                }
            }