Flex功能未显示文件对话框

时间:2013-01-22 10:08:10

标签: actionscript-3 flex externalinterface

我正在尝试使用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的标签中的文字已更改。

1 个答案:

答案 0 :(得分: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();
            }
        }