0

我正在尝试使用 ExternalInterface 和 addCallback 从 javascript 执行 flex 函数:

<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();
}

但由于某些原因,文件对话框未显示,但 ID 为 l1 的标签中的文本已更改。

4

1 回答 1

2

FileReference.browse操作只能在响应用户操作(鼠标事件或按键事件)时调用,因此您必须修改代码以获得用户操作,例如您可以使用警报:

        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();
            }
        }
于 2013-01-22T10:29:28.190 回答