先看下整体思路,整个上传,以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主要作为上传图片的主要工具,并且实现了客户端和服务端的即时数据更新。
时间: 2024-11-06 21:18:34