標籤:asp.net flash mvc 圖片 上傳
先看下整體思路,整個上傳,以flash按鈕為入口
建立Flash,添加一個按鈕,並命名為btn
添加類main.as
package {import flash.display.*;import flash.events.*;import flash.net.*;import flash.ui.*;import flash.external.ExternalInterface;public class main extends Sprite {public var btn:SimpleButton;private var uid:String;private var jid:String;private var sn:String;//檔案上傳private var file:FileReference=new FileReference();//過濾private var images_filter:FileFilter = new FileFilter("*.jpg, *.jpeg, *.gif, *.png", "*.jpg;*.jpeg;*.gif;*.png");//上傳地址private var uploadURL:URLRequest;//建構函式public function main() {////隱藏預設菜單 var contextmenu:ContextMenu = new ContextMenu; contextmenu.hideBuiltInItems();//這個參數很重要,後面會提到,重要解決sessionID不一致的問題uid = stage.loaderInfo.parameters.ASPSESSID;jid = stage.loaderInfo.parameters.AUTHID;//提交地址uploadURL = new URLRequest("/SwfUploadImg/?ASPSESSID=" + uid + "&AUTHID=" + jid);//情境點擊事件btn.addEventListener(MouseEvent.CLICK, clickHandler);//選擇圖片後事件file.addEventListener(Event.SELECT, selectedHandler);file.addEventListener(Event.OPEN, openHandler); file.addEventListener(ProgressEvent.PROGRESS, progressHandler); file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler);}//點擊按鈕事件private function clickHandler(evt:MouseEvent):void{//顯示選擇圖片對話方塊file.browse([images_filter]);}//圖片選擇後事件private function selectedHandler(e:Event):void{ExternalInterface.call("Laugh_UpImg_onSelected",file.name,file.size,file.type);//開始上傳圖片file.upload(uploadURL,"Filedata");}//開始上傳private function openHandler(event:Event):void{ExternalInterface.call("Laugh_UpImg_onStart");}//上傳完成private function uploadCompleteDataHandler(event:DataEvent):void{ExternalInterface.call("Laugh_UpImg_onComplete" , event.data);}//上傳進度private function progressHandler(event:ProgressEvent):void{ExternalInterface.call("Laugh_UpImg_onProgress",event.bytesLoaded,event.bytesTotal);}//上傳錯誤private function ioErrorHandler(event:IOErrorEvent):void{ExternalInterface.call("Laugh_UpImg_onError",event.text);}//上傳許可權錯誤private function securityErrorHandler(event:SecurityErrorEvent):void{ExternalInterface.call("Laugh_UpImg_onSecurityError",event.text);}}}
代碼解釋:
ExternalInterface.call
調用外部JS代碼,第一個參數為JS的函數名,後面一個參數為函數的實參。
根據注釋可以判斷出每個方法的作用。
stage.loaderInfo.parameters.ASPSESSID
接受頁面傳來的參數,用於提交給.net頁面進行許可權的驗證
flash主要作為上傳圖片的主要工具,並且實現了用戶端和服務端的即時資料更新。
FLASH實現ASP.NET MVC上傳---Flash篇