1. 程式人生 > >Flex呼叫js方法傳遞引數

Flex呼叫js方法傳遞引數

在上傳完檔案以後需要將檔案資訊載入到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 新增背景圖片將在後面的部落格中敘述。