0

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.

croppio.com
  • 1,823
  • 5
  • 28
  • 44

1 Answers1

2

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();
            }
        }
fsbmain
  • 5,267
  • 2
  • 16
  • 23
  • I see, but i want to open the file dialog (FileReference) from clicking a javascript button and not interacting directly with flash, but indirectly with ExternalInterface. Or there is a method to simulate a user action? – croppio.com Jan 22 '13 at 10:41
  • 1
    No, the user action must be in the call stack in the flash, you have to organize the application logic and UI according to this requirement. There aren't way to simulate user action or show the browser dialog without such action, it's for security and privacy reasons. – fsbmain Jan 22 '13 at 10:45