<?xml version="1.0" encoding="utf-8"?> <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" width="600" height="400" xmlns:net="flash.net.*" initialize="initEvent(event)" creationComplete="creationCompleteEvent(event)" xmlns:local="*"> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <net:FileReferenceList id="fileReferenceList" select="fileReference_select(event);"/> <mx:NumberFormatter id="filesizeformatter" useThousandsSeparator="true"/> </fx:Declarations> <fx:Script> <![CDATA[ import flash.net.URLRequest; import flash.text.Font; import flashx.textLayout.formats.Float; import mx.binding.utils.BindingUtils; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.controls.ProgressBar; import mx.events.CollectionEvent; import mx.formatters.CurrencyFormatter; import mx.formatters.NumberFormatter; import org.bytearray.gif.player.GIFPlayer; private var FileDesc:String="Images(*.jpg;*gif;*.png;*.JPG;*.GIF;*.PNG)"; private var Extension:String="*.jpg;*gif;*.png;*.JPG;*.GIF;*.PNG"; private var Url:String=""; private var FileFolder:String=""; //public var angle:int=0; private var urlrequest: URLRequest = new URLRequest(Url); private var singleThreadFiles: Array = []; private var file:FileReference; //要上传的文件 private var arrColl:ArrayCollection = new ArrayCollection([]); [Bindable] public var angle:int=0; private var oldX:Number; //拖动开始是的坐标 private var oldY:Number; private var _myGIFPlayer:GIFPlayer = new GIFPlayer(); private var request:URLRequest; private var count:int=0; [Bindable] private var size:Number=0.0; private var ac:ArrayCollection = new ArrayCollection([]); private function initEvent(e:Event):void { Url =ExternalInterface.call("setFlexUrl"); var sUrl:String = "loading.gif"//ExternalInterface.call("setFlexGIFUrl"); request = new URLRequest(sUrl); _myGIFPlayer.load(request); img.addChild(_myGIFPlayer); addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler); //var aa:Boolean=typeof(root.loaderInfo.parameters["fileDesc"])=="undefined"; } private function creationCompleteEvent(e:Event):void { //var aa:Boolean=typeof(LoaderInfo(this.root.loaderInfo).parameters["fileDesc"])=="undefined"; } private function btn_click(evt:MouseEvent):void { var arr:Array = []; arr.push(new FileFilter(FileDesc,Extension)); fileReferenceList.browse(arr); } private function fileReference_select(evt:Event):void { angle = arrColl.length+fileReferenceList.fileList.length; for (var i:int = 0; i < fileReferenceList.fileList.length; i++){ if(angle <=15){ fileReferenceList.fileList[i].addEventListener(Event.COMPLETE,completeLoadFile); fileReferenceList.fileList[i].load(); }else{ Alert.show("图片一次最多上传15张"); return; } } } private function completeLoadFile(e:Event):void { var loader:Loader = new Loader(); var file:FileReference = e.target as FileReference; if(file.size>1024*1024*5) { Alert.show("文件不能超过5M.","错误"); return; } loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function():void { var bmp:Bitmap=loader.content as Bitmap; var bmd:BitmapData; if((bmp.width>bmp.height||bmp.width==bmp.height)&&bmp.width>800) { var rateW:Number=800/bmp.width; var h:Number=rateW*bmp.height; bmd=new BitmapData(800,h,true,0); bmd.draw(bmp.bitmapData,new Matrix(rateW,rateW),null,true); bmp=new Bitmap(bmd,"auto",true); } else if(bmp.height>bmp.width&&bmp.height>800) { var rateH:Number=800/bmp.height; var w:Number=rateH*bmp.width; bmd=new BitmapData(w,800,new Matrix(rateH,rateH),true); } var f:FileReference = e.target as FileReference; f.removeEventListener(Event.COMPLETE,completeLoadFile); size +=f.size/1024/1024; size = new Number(size.toFixed(2)); arrColl.addItem({img:bmp,file:f}); }); loader.loadBytes(e.target.data); } public function removeFile(data:Object): void { var index: int = arrColl.getItemIndex(data); var f:FileReference = data.file as FileReference; size = size-f.size/1024/1024; size = new Number(size.toFixed(2)); if(size <0){ size =0; } if (index != -1 && index>0){ arrColl.removeItemAt(index); }else{ arrColl.removeItemAt(0); /* for(var i:int =0;i<arrColl.length;i++){ ac.addItem(arrColl.getItemAt(i)); } */ //ceshi.source = ac.getItemAt(0).img; //tileList.dataProvider = ac; // arrColl.enableAutoUpdate(); /* if(s &&arrColl.length > 0){ var o:Object=arrColl[0]; var bmp:Bitmap=o.img as Bitmap; /* var bmd:BitmapData; if((bmp.width>bmp.height||bmp.width==bmp.height)&&bmp.width>800) { var rateW:Number=800/bmp.width; var h:Number=rateW*bmp.height; bmd=new BitmapData(800,0); bmd.draw(bmp.bitmapData,true); bmp=new Bitmap(bmd,true); } else if(bmp.height>bmp.width&&bmp.height>800) { var rateH:Number=800/bmp.height; var w:Number=rateH*bmp.width; bmd=new BitmapData(w,true); } var files:FileReference = o.file; arrColl.setItemAt({img:bmp,file:files},0); //arrColl.refresh(); } */ } } private function uploadFile(): void { for each (var obj:Object in arrColl) { try { var f:FileReference = obj.file; f.upload(urlrequest); } catch (e: Error) { Alert.show(e.message); } } } private function uploadFileOneByOne():void { addimg.visible = true; var sl:int = arrColl.length; if(sl ==0){ Alert.show("请您添加图片"); addimg.visible = false; return; } if(typeof(Url)!="undefined") { var jssessionId:String=ExternalInterface.call("getSession");//";ddfdf=ff"// urlrequest = new URLRequest(Url+jssessionId); } if(typeof(FileFolder)!="undefined") { FileFolder =ExternalInterface.call("getRequestVal"); } if(urlrequest.url==null || urlrequest.url=="") { Alert.show("请求的地址不能为空.","错误"); addimg.visible = false; return; } //var getSessionKey:String = ExternalInterface.call("getSessionKey"); //var getSessionVal:String = ExternalInterface.call("getSessionVal"); var variables:urlvariables = new urlvariables(); //variables['canshu'] = FileFolder; //variables[getSessionKey]=getSessionVal; urlrequest.data=FileFolder; urlrequest.method = URLRequestMethod.GET; count = arrColl.length; for each (var obj:Object in arrColl) { singleThreadFiles.push(obj.file); } singleThreadFiles.reverse(); singleThreadUploadFile(); } private function singleThreadUploadFile(): void { img.visible = true; masks.visible = true; //FIFO:逐个从列表中取出,进行同步上传 if (singleThreadFiles.length > 0) { var f: FileReference = singleThreadFiles.pop() as FileReference; f.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler); f.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,doSingleUploadFileComplete); f.upload(urlrequest); }else{ img.visible = false; masks.visible = false; ExternalInterface.call("onUploaderComplete",count); // 每上传完一个文件后调用js函数onUploaderComplete } } private function ioErrorHandler(o:Object):void{ img.visible = false; masks.visible = false; addimg.visible = false; Alert.show(o.text.toString()); } private function doSingleUploadFileComplete(event: Event): void { var f: FileReference = event.target as FileReference; f.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,doSingleUploadFileComplete); var s:Object = arrColl.removeItemAt(0); if(s &&arrColl.length > 0){ var o:Object=arrColl[0]; var bmp:Bitmap=o.img as Bitmap; var bmd:BitmapData; if((bmp.width>bmp.height||bmp.width==bmp.height)&&bmp.width>800) { var rateW:Number=800/bmp.width; var h:Number=rateW*bmp.height; bmd=new BitmapData(800,true); } var files:FileReference = o.file; arrColl.setItemAt({img:bmp,0); } singleThreadUploadFile(); } private function RemoveAllFiles(event:Event):void { arrColl.removeAll(); size = 0; } //向右旋转90度 public function scaleRight(data:Object):void{ var index: int = arrColl.getItemIndex(data); var bmp:Bitmap=data.img as Bitmap; var bmd:BitmapData; var rateW:Number=120; var h:Number=100; bmd=new BitmapData(rateW,0); var m:Matrix =data.img.transform.matrix; var offsetWidth:Number = rateW / 2; var offsetHeight:Number = h / 2; m.translate(-offsetWidth,-offsetHeight); m.rotate(Math.PI / 2); m.translate(+offsetWidth,+offsetHeight); bmd.draw(bmp.bitmapData,m,true); bmp=new Bitmap(bmd,true); var files:FileReference = data.file; arrColl.setItemAt({img:bmp,index); /**var m:Matrix = new Matrix(); m.rotate(Math.PI/2); m.translate(bmp.height,0); var bd:BitmapData = new BitmapData(bmp.height,bmp.width); bd.draw(bmp,m); return bd; **/ } private function showupload(event:MouseEvent):void{ pl.visible = false; panel.visible = true; btn_click(event); } ]]> </fx:Script> <mx:Panel id="pl" layout="vertical" horizontalCenter="0" verticalCenter="0" width="600" height="400" headerHeight="0" borderVisible="true" visible="true" horizontalAlign="center" verticalAlign="middle" borderColor="#DCDCDC" > <mx:Image source="click.png" buttonMode="true" click="showupload(event)"> </mx:Image> <mx:Text text="请点击添加图片按钮"> </mx:Text> </mx:Panel> <mx:Panel id="panel" layout="vertical" horizontalCenter="0" verticalCenter="0" width="600" height="400" headerHeight="0" borderVisible="false" visible="false"> <s:Group> <s:Group> <mx:TileList id="tileList" dataProvider="{arrColl}" allowMultipleSelection="true" width="580" height="350" verticalScrollPolicy="off" dragEnabled="false" x="0" y="0" borderColor="#DCDCDC"> <mx:itemRenderer> <fx:Component> <mx:VBox> <s:Group> <!--<mx:LinkButton label="()" accentColor="#cdddf3"> <mx:click> <![CDATA[ outerDocument.scaleRight(data); ]]> </mx:click> </mx:LinkButton> --> <mx:Image width="108" height="100" source="{data.img}"/> <s:Group y="82" width="108" height="20"> <s:Rect alpha="0.5" width="100%" height="100%" id="reId"> <s:fill> <s:LinearGradient rotation="250"> <s:GradientEntry color="0x222222" ratio="0"/> <!--<s:GradientEntry color="0xdddddd" ratio="1"/>--> </s:LinearGradient> </s:fill> </s:Rect> <mx:Text text="删除" width="108" id="butId" mouseChildren="false" buttonMode="true" color="#FEFCFC" > <mx:click> <![CDATA[ outerDocument.removeFile(data); ]]> </mx:click> </mx:Text> <!--<mx:LinkButton label="×" accentColor="#cdddf3" visible="false" id="butId" name="butId"> <mx:click> <![CDATA[ outerDocument.removeFile(data); ]]> </mx:click> </mx:LinkButton> --> </s:Group> </s:Group> </mx:VBox> </fx:Component> </mx:itemRenderer> </mx:TileList> </s:Group> <s:Group left="0" y="0"> <s:Group visible="false" width="115" height="110" alpha="0.6" id="masks" x="0" y="0"> <s:Rect alpha="0.5" width="100%" height="100%"> <s:fill> <s:LinearGradient rotation="250"> <s:GradientEntry color="0x222222" ratio="0"/> <s:GradientEntry color="0xdddddd" ratio="1"/> </s:LinearGradient> </s:fill> </s:Rect> </s:Group> <mx:Image id="img" x="20" y="40" alpha="0.6" visible="false"/> </s:Group> </s:Group> <mx:HBox width="582" verticalAlign="top" > <mx:Label text="共{arrColl.length}张图片,共{size}M" width="70%"> </mx:Label> <s:Group width="60"> <s:Group> <mx:Image source="addimg.jpg" click="btn_click(event);" buttonMode="true"> </mx:Image> </s:Group> <s:Group width="62" height="20" alpha="0.6" x="0" y="0" id="addimg" visible="false"> <s:Rect alpha="0.5" width="100%" height="100%" > <s:fill> <s:LinearGradient rotation="250"> <s:GradientEntry color="0x222222" ratio="0"/> <s:GradientEntry color="0xdddddd" ratio="1"/> </s:LinearGradient> </s:fill> </s:Rect> </s:Group> </s:Group> <mx:Image source="updata.jpg" buttonMode="true"> <mx:click> <![CDATA[ uploadFileOneByOne(); ]]> </mx:click> </mx:Image> <mx:Image source="detall.jpg" click="RemoveAllFiles(event);" buttonMode="true"> </mx:Image> </mx:HBox> </mx:Panel> </s:Application> 哪位大大知道原因么.......