Flex调用js方法传递参数

来源:互联网 发布:ember.js 视频教程 编辑:程序博客网 时间:2024/05/08 01:53

在上传完文件以后需要将文件信息加载到session中,但是在监听器中没有使用filter拦截,所以决定把文件的关键字传递到jsp页面然后在通过页面调用action。这样上传文件的信息就可以写入session中了。

开始没有接触过这方面内容,从网络上搜索了一下这个功能实现起来还是比较简单的。

首先要在FLex文件中引入import flash.external.ExternalInterface;关于ExternalInterface解释为: The ExternalInterface class is an application programming interface that enables straightforward communication between ActionScript and the SWF container– for example, an HTML page with JavaScript or a desktop application that uses Flash Player to display a SWF file.

Using the ExternalInterface class, you can call an ActionScript function in the Flash runtime, using JavaScript in the HTML page. The ActionScript function can return a value, and JavaScript receives it immediately as the return value of the call.(摘自:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#

然后在as代码中添加如下代码

private function progressBar_complete(evt:Event):void { //向js传信息initApp();//调用写入session消息callJs();}  public function myFunc():String {return fileRef.name +"/" + fileRef.size + "/" +fileComment.text + "/" + fileRef.type;}public function initApp():void {ExternalInterface.addCallback("myFlexFunction",myFunc);}public function callJs():void{var flexToJs:String = ExternalInterface.call("callAction","writeSession");}

progressBar_complete是进度条到达100%以后的效果,代码意思就是当进度条到达100%(文件上传完成以后)调用js方法

initApp 是像js传递参数,参数就是myFunc的返回值



    addCallBack()方法:

public static function addCallback(functionName:String, closure:Function):void


Registers an ActionScript method as callable from the container. After a successful invocation of addCallBack(), the registered function in the player can be called by JavaScript or ActiveX code in the container.



addCallback 像js传值

Call方法:
public static function call(functionName:String... arguments):*


Calls a function exposed by the SWF container, passing zero or more arguments. If the function is not available, the call returns null; otherwise it returns the value provided by the function. Recursion is not permitted on Opera or Netscape browsers; on these browsers a recursive call produces a null response. (Recursion is supported on Internet Explorer and Firefox browsers.)

If the container is an HTML page, this method invokes a JavaScript function in a script element.

If the container is another ActiveX container, this method dispatches the FlashCall ActiveX event with the specified name, and the container processes the event.

If the container is hosting the Netscape plug-in, you can either write custom support for the new NPRuntime interface or embed an HTML control and embed the player within the HTML control. If you embed an HTML control, you can communicate with the player through a JavaScript interface to the native container application.

     call 调用js方法,可以向js传递三层也可以得到js返回值

         var flexToJs:String = ExternalInterface.call("callAction","canshu");
       此方法是调用js方法callAction方法,并向其传参数"canshu",得到返回值flexTojs

Flex和Js相互调用有利于传递参数,例如Flex可以传递文件信息给js,js同样可以传递给Flex一些信息

另外还有给文件上传添加背景图片的代码,样式如下:

如何给Flex 添加背景图片将在后面的博客中叙述。

 

  
0 0