使用FLEX进行多文件上传和自定义上传信息
2009-01-19 11:55:04 来源:WEB开发网参数complete和progress分别是上传成功和上传进度回调方法。
窗体代码
<?xmlversion="1.0"encoding="utf-8"?>
<mx:TitleWindowxmlns:mx="http://www.adobe.com/2006/mxml"layout="vertical"width="628"height="300"title="上传文件"showCloseButton="true"xmlns:net="flash.net.*">
<mx:close>
<![CDATA[
OnWorkingChange();
PopUpManager.removePopUp(this);
]]>
</mx:close>
<mx:Canvaswidth="100%"height="100%">
<mx:VBoxleft="10"right="20"verticalAlign="middle"horizontalAlign="center">
<mx:Repeater dataProvider="{mUploadFiles}"id="rp"width="100%"height="100%">
<mx:HBoxdata="{rp.currentItem}" verticalAlign="middle"height="26"horizontalAlign="center">
<mx:render>
<![CDATA[
varhb:HBox=HBox(event.target);
varpb:ProgressBar=ProgressBar(hb.getChildByName("pb1"));
varuf:UploadFile=UploadFile(hb.data);
pb.setProgress(uf.LoadSize,uf.Size);
]]>
</mx:render>
<mx:Button visible="{rp.currentItem.State!=UploadFile.COMPLETE}"data="{rp.currentIndex}"icon="@Embed(source='http://www.cnblogs.com/Imanges/delete16.png')"width="28">
<mx:click>
<![CDATA[
DeleteUploadItem(int(event.target.data));
]]>
</mx:click>
</mx:Button>
<mx:Label width="180"text="{rp.currentItem.Name}"/>
<mx:TextInputdata="{rp.currentItem}"text="{rp.currentItem.Title}"width="206">
<mx:change>
<![CDATA[
event.target.data.Title=event.target.text;
]]>
</mx:change>
</mx:TextInput>
<mx:ProgressBar id="pb1" labelPlacement="center" mode="manual" width="106"fontWeight="normal"/>
</mx:HBox>
<mx:HRulewidth="100%"/>
</mx:Repeater>
</mx:VBox>
</mx:Canvas>
<mx:ControlBarhorizontalAlign="right"verticalAlign="middle">
<mx:Buttonlabel="上传">
<mx:click>
<![CDATA[
Upload_File();
]]>
</mx:click>
</mx:Button>
<mx:Buttonlabel="确定">
<mx:click>
<![CDATA[
OnWorkingChange();
PopUpManager.removePopUp(this);
]]>
</mx:click>
</mx:Button>
</mx:ControlBar>
<mx:initialize>
<![CDATA[
My=this;
]]>
</mx:initialize>
<mx:Script>
<![CDATA[
importCodes.PopUpEffect;
importmx.core.IFlexDisplayObject;
importmx.managers.PopUpManager;
importmx.controls.ProgressBar;
importCodes.UploadFile;
importCodes.SysConfig;
importflash.net.URLRequest;
importCodes.Untity;
importflash.net.URLVariables;
importflash.net.FileReference;
importHFSoftFx.HFSoftCoreFun;
privatevarMy:IFlexDisplayObject;
[Bindable]
privatevarmUploadFiles:Array=newArray();
privatevarintervalId:uint;
publicvarWorkingChange:Function;
privatefunctionOnWorkingChange():void{
if(WorkingChange!=null)
WorkingChange();
}
publicfunctiongetUploadFiles():Array
{
returnmUploadFiles;
}
privatefunctionDeleteUploadItem(index:int)
{
varitem:UploadFile =UploadFile(mUploadFiles[index]);
item.FileRef.cancel();
mUploadFiles.splice(index,1);
OnWorkingChange();
rp.executeBindings(true);
if(mUploadFiles.length==0)
PopUpEffect.Hide(My);
}
publicfunctionUpload_File():void{
varitem:UploadFile =UploadFile(mUploadFiles[0]);
varrequest:URLRequest=Untity.GetUploadFileRequest();
varvariables:URLVariables=new URLVariables();
variables.Title=item.Title;
variables.FolderID=item.FolderID;
request.data=variables;
item.Upload(request,function(){
DeleteUploadItem(0);
if(mUploadFiles.length>0)
intervalId=flash.utils.setInterval(function(){
flash.utils.clearInterval(intervalId);
Upload_File();
},500,null);
},function(){
rp.executeBindings(true);
});
}
publicfunctionAddItem(files:FileReferenceList,folderid:int):void{
varitem:UploadFile;
var fr:FileReference;
for(vari:uint=0;i<files.fileList.length;i++){
fr=FileReference(files.fileList[i]);
if(fr.size>SysConfig.ImageMaxSize)
{
HFSoftCoreFun.AlertError(fr.name+"文件大于"+(SysConfig.ResourceFileMaxSize/1024)+"(KB)");
break;
}
item=newUploadFile();
item.FileRef=fr;
item.FolderID=folderid;
item.Title=fr.name;
item.Name=fr.name;
item.Size=fr.size;
mUploadFiles.push(item);
}
rp.executeBindings(true);
}
]]>
</mx:Script>
</mx:TitleWindow>
更多精彩
赞助商链接