AS3批量上传使用PureMvc框架的例子
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="500" height="500" creati>
<mx:Script>
<![CDATA[
import com.ApplicationFacade;
private var facade:ApplicationFacade=ApplicationFacade.getInstance();
private function init():void{
facade.startup(this);
}
]]>
</mx:Script>
</mx:Application>
ApplicationFacade:
package com
{
import com.controller.ApplicationCommand;
import com.controller.UploadCommand;
import org.puremvc.as3.interfaces.IFacade;
import org.puremvc.as3.patterns.facade.Facade;
public class ApplicationFacade extends Facade implements IFacade
{
public static const STARTUP:String="startup";
public static const UPLOAD:String="upload";
public function ApplicationFacade()
{
}
public static function getInstance():ApplicationFacade{
if(instance==null){
instance=new ApplicationFacade();
}
return instance as ApplicationFacade;
}
override protected function initializeController():void{
super.initializeController();
registerCommand(ApplicationFacade.STARTUP, ApplicationCommand);
registerCommand(ApplicationFacade.UPLOAD, UploadCommand);
}
public function startup(app:UploadFile):void{
sendNotification(ApplicationFacade.STARTUP,app);
}
}
}
view下UI:
<?xml version="1.0" encoding="utf-8"?>
<mx anel layout="absolute" xmlns:mx="http://www.adobe.com/2006/mxml" width="500" height="500" title="上传卡片">
<mx:Button x="124" y="10" label="主题类型卡" click="dispatchEvent(new Event(UploadForm.SUBTYPE))"/>
<mx:Button x="243" y="10" label="热门类型卡" click="dispatchEvent(new Event(UploadForm.HOTTYPE))"/>
<mx abel x="112" y="63" text="上传JPG" click="dispatchEvent(new Event(UploadForm.UPLOAD_JPG))"/>
<mx abel x="275" y="63" text="上传SWF" click="dispatchEvent(new Event(UploadForm.UPLOAD_SWF))"/>
<mx anel y="131" x="251" width="229" height="190" layout="absolute" title="上传卡片具体类型" id="typePanel">
<mx:RadioButtonGroup id="cardType"/>
<mx:RadioButton label="生日卡" groupName="cardType" value="1"/>
<mx:RadioButton label="感谢卡" x="126" y="40" groupName="cardType" value="2"/>
<mx:RadioButton label="道歉卡" x="63" y="82" groupName="cardType" value="3"/>
<mx:RadioButton label="问候卡" x="0" y="82" groupName="cardType" value="4"/>
<mx:RadioButton label="节庆卡" x="63" y="40" groupName="cardType" value="5"/>
<mx:RadioButton label="示爱卡" x="0" y="40" groupName="cardType" value="6"/>
<mx:RadioButton label="邀请卡" x="126" y="0" groupName="cardType" value="7"/>
<mx:RadioButton label="祝贺卡" x="126" groupName="cardType" y="82" value="8"/>
<mx:RadioButton label="特殊卡" x="63" y="0" groupName="cardType" value="9"/>
</mx anel>
<mx:Button x="166" y="414" label="开始上传文件" click="dispatchEvent(new Event(UploadForm.UPLOAD))" />
<mx:Script>
<![CDATA[
public static const UPLOAD_SWF:String="uploadSwf";
public static const UPLOAD_JPG:String="uploadJpg";
public static const UPLOAD:String="upload";
public static const HOTTYPE:String="hotType";
public static const SUBTYPE:String="subType";
]]>
</mx:Script>
<mx:TextArea x="26" y="110" height="250" width="179" id="txt"/>
</mx anel>
view
package com.view
{
import com.ApplicationFacade;
import com.model.UploadProxy;
import com.model.vo.UploadVo;
import com.view.ui.UploadForm;
import flash.events.Event;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import org.puremvc.as3.interfaces.IMediator;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.mediator.Mediator;
public class UploadMediator extends Mediator implements IMediator
{
public static const NAME:String="UploadMediator";
private var upload:UploadFile;
private var uploadForm:UploadForm;
private var fileList:FileReferenceList;
public function UploadMediator(app:Object)
{
super(UploadMediator.NAME, app);
upload=app as UploadFile;
uploadForm=new UploadForm();
fileList=new FileReferenceList();
fileList.addEventListener(Event.SELECT,onSelect);
uploadForm.addEventListener(UploadForm.UPLOAD_JPG,onUploadJpg);
uploadForm.addEventListener(UploadForm.UPLOAD_SWF,onUploadSwf);
uploadForm.addEventListener(UploadForm.UPLOAD,onUpload);
upload.addChild(uploadForm);
uploadForm.addEventListener(UploadForm.HOTTYPE,onHot);
uploadForm.addEventListener(UploadForm.SUBTYPE,onSub);
}
private function onHot(e:Event):void{
this.uploadForm.typePanel.visible=false;
}
private function onSub(e:Event):void{
this.uploadForm.typePanel.visible=true;
}
private function onUploadJpg(e:Event):void{
var filter:FileFilter=new FileFilter("Images", "*.jpg;*.gif;*.png");
fileList.browse([filter]);
}
private function onUploadSwf(e:Event):void{
var filter:FileFilter=new FileFilter("SWF","*.swf");
fileList.browse([filter]);
}
private function onSelect(e:Event):void{
if(fileList.fileList.length>0){
var file:FileReference;
for (var i:uint = 0; i < this.fileList.fileList.length; i++) {
file = FileReference(this.fileList.fileList );
file.load();
}
}
}
private function onUpload(e:Event):void{
if(fileList.fileList.length>0){
var vo:UploadVo=new UploadVo();
vo.cardFlag=this.uploadForm.typePanel.visible;
vo.cardType=this.uploadForm.cardType.selectedValue as int ;
vo.fileList=this.fileList;
sendNotification(ApplicationFacade.UPLOAD,vo)
}
}
override public function handleNotification(notification:INotification):void{
switch(notification.getName()){
case UploadProxy.UPLOAD_SUCCESS:
setObj(notification.getBody() as Object)
break;
case UploadProxy.UPLOAD_FAILED:
setObj(notification.getBody() as Object)
break;
default:
break;
}
}
private function setObj(obj:Object):void{
this.uploadForm.txt.text+=obj.result+"\n";
}
override public function listNotificationInterests():Array{
var arr:Array=new Array();
arr.push(UploadProxy.UPLOAD_SUCCESS);
arr.push(UploadProxy.UPLOAD_FAILED);
return arr;
}
}
}
model 下VO
package com.model.vo
{
import flash.net.FileReferenceList;
public class UploadVo
{
public var fileList:FileReferenceList;
public var cardType:int;
public var cardFlag:Boolean;
public function UploadVo()
{
}
}
}
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="500" height="500" creati>
<mx:Script>
<![CDATA[
import com.ApplicationFacade;
private var facade:ApplicationFacade=ApplicationFacade.getInstance();
private function init():void{
facade.startup(this);
}
]]>
</mx:Script>
</mx:Application>
ApplicationFacade:
package com
{
import com.controller.ApplicationCommand;
import com.controller.UploadCommand;
import org.puremvc.as3.interfaces.IFacade;
import org.puremvc.as3.patterns.facade.Facade;
public class ApplicationFacade extends Facade implements IFacade
{
public static const STARTUP:String="startup";
public static const UPLOAD:String="upload";
public function ApplicationFacade()
{
}
public static function getInstance():ApplicationFacade{
if(instance==null){
instance=new ApplicationFacade();
}
return instance as ApplicationFacade;
}
override protected function initializeController():void{
super.initializeController();
registerCommand(ApplicationFacade.STARTUP, ApplicationCommand);
registerCommand(ApplicationFacade.UPLOAD, UploadCommand);
}
public function startup(app:UploadFile):void{
sendNotification(ApplicationFacade.STARTUP,app);
}
}
}
view下UI:
<?xml version="1.0" encoding="utf-8"?>
<mx anel layout="absolute" xmlns:mx="http://www.adobe.com/2006/mxml" width="500" height="500" title="上传卡片">
<mx:Button x="124" y="10" label="主题类型卡" click="dispatchEvent(new Event(UploadForm.SUBTYPE))"/>
<mx:Button x="243" y="10" label="热门类型卡" click="dispatchEvent(new Event(UploadForm.HOTTYPE))"/>
<mx abel x="112" y="63" text="上传JPG" click="dispatchEvent(new Event(UploadForm.UPLOAD_JPG))"/>
<mx abel x="275" y="63" text="上传SWF" click="dispatchEvent(new Event(UploadForm.UPLOAD_SWF))"/>
<mx anel y="131" x="251" width="229" height="190" layout="absolute" title="上传卡片具体类型" id="typePanel">
<mx:RadioButtonGroup id="cardType"/>
<mx:RadioButton label="生日卡" groupName="cardType" value="1"/>
<mx:RadioButton label="感谢卡" x="126" y="40" groupName="cardType" value="2"/>
<mx:RadioButton label="道歉卡" x="63" y="82" groupName="cardType" value="3"/>
<mx:RadioButton label="问候卡" x="0" y="82" groupName="cardType" value="4"/>
<mx:RadioButton label="节庆卡" x="63" y="40" groupName="cardType" value="5"/>
<mx:RadioButton label="示爱卡" x="0" y="40" groupName="cardType" value="6"/>
<mx:RadioButton label="邀请卡" x="126" y="0" groupName="cardType" value="7"/>
<mx:RadioButton label="祝贺卡" x="126" groupName="cardType" y="82" value="8"/>
<mx:RadioButton label="特殊卡" x="63" y="0" groupName="cardType" value="9"/>
</mx anel>
<mx:Button x="166" y="414" label="开始上传文件" click="dispatchEvent(new Event(UploadForm.UPLOAD))" />
<mx:Script>
<![CDATA[
public static const UPLOAD_SWF:String="uploadSwf";
public static const UPLOAD_JPG:String="uploadJpg";
public static const UPLOAD:String="upload";
public static const HOTTYPE:String="hotType";
public static const SUBTYPE:String="subType";
]]>
</mx:Script>
<mx:TextArea x="26" y="110" height="250" width="179" id="txt"/>
</mx anel>
view
package com.view
{
import com.ApplicationFacade;
import com.model.UploadProxy;
import com.model.vo.UploadVo;
import com.view.ui.UploadForm;
import flash.events.Event;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import org.puremvc.as3.interfaces.IMediator;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.mediator.Mediator;
public class UploadMediator extends Mediator implements IMediator
{
public static const NAME:String="UploadMediator";
private var upload:UploadFile;
private var uploadForm:UploadForm;
private var fileList:FileReferenceList;
public function UploadMediator(app:Object)
{
super(UploadMediator.NAME, app);
upload=app as UploadFile;
uploadForm=new UploadForm();
fileList=new FileReferenceList();
fileList.addEventListener(Event.SELECT,onSelect);
uploadForm.addEventListener(UploadForm.UPLOAD_JPG,onUploadJpg);
uploadForm.addEventListener(UploadForm.UPLOAD_SWF,onUploadSwf);
uploadForm.addEventListener(UploadForm.UPLOAD,onUpload);
upload.addChild(uploadForm);
uploadForm.addEventListener(UploadForm.HOTTYPE,onHot);
uploadForm.addEventListener(UploadForm.SUBTYPE,onSub);
}
private function onHot(e:Event):void{
this.uploadForm.typePanel.visible=false;
}
private function onSub(e:Event):void{
this.uploadForm.typePanel.visible=true;
}
private function onUploadJpg(e:Event):void{
var filter:FileFilter=new FileFilter("Images", "*.jpg;*.gif;*.png");
fileList.browse([filter]);
}
private function onUploadSwf(e:Event):void{
var filter:FileFilter=new FileFilter("SWF","*.swf");
fileList.browse([filter]);
}
private function onSelect(e:Event):void{
if(fileList.fileList.length>0){
var file:FileReference;
for (var i:uint = 0; i < this.fileList.fileList.length; i++) {
file = FileReference(this.fileList.fileList );
file.load();
}
}
}
private function onUpload(e:Event):void{
if(fileList.fileList.length>0){
var vo:UploadVo=new UploadVo();
vo.cardFlag=this.uploadForm.typePanel.visible;
vo.cardType=this.uploadForm.cardType.selectedValue as int ;
vo.fileList=this.fileList;
sendNotification(ApplicationFacade.UPLOAD,vo)
}
}
override public function handleNotification(notification:INotification):void{
switch(notification.getName()){
case UploadProxy.UPLOAD_SUCCESS:
setObj(notification.getBody() as Object)
break;
case UploadProxy.UPLOAD_FAILED:
setObj(notification.getBody() as Object)
break;
default:
break;
}
}
private function setObj(obj:Object):void{
this.uploadForm.txt.text+=obj.result+"\n";
}
override public function listNotificationInterests():Array{
var arr:Array=new Array();
arr.push(UploadProxy.UPLOAD_SUCCESS);
arr.push(UploadProxy.UPLOAD_FAILED);
return arr;
}
}
}
model 下VO
package com.model.vo
{
import flash.net.FileReferenceList;
public class UploadVo
{
public var fileList:FileReferenceList;
public var cardType:int;
public var cardFlag:Boolean;
public function UploadVo()
{
}
}
}