前几天写了一篇jsp页面利用ajaxFileUpload上传文件,现在把flex上传页面也分享出来:
前台页面
<?xml version="1.0" encoding="utf-8"?>
<s:HGroup xmlns:fx="<a target=_blank href="http://ns.adobe.com/mxml/2009">http://ns.adobe.com/mxml/2009</a>"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="hgroup1_creationCompleteHandler(event)"
width="100%" height="30" >
<fx:Script>
<![CDATA[
import mx.collections.ArrayList;
import mx.controls.Alert;
import mx.events.FlexEvent;
private var file:FileReference = new FileReference;
public var fileList:ArrayList;
public var manualCheck:ManualCheck;
protected function hgroup1_creationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
// file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,fileUploadCompleteHandler);
file.addEventListener(Event.SELECT, fileSelect);
// file.addEventListener(IOErrorEvent.IO_ERROR,uploadError);
}
protected function button2_clickHandler(event:MouseEvent):void
{
// 浏览
file.browse();
}
private function fileSelect(e:Event):void
{
if(fileList.getItemIndex(file) == -1){
fileList.addItem(file);
}
fileName.text = file.name;
}
// private function fileUploadCompleteHandler(e:DataEvent):void{
//
// }
private function uploadError(e:IOErrorEvent):void{
this.cursorManager.removeBusyCursor();
//获取后台的错误提示信息
Alert.show("上传出错。","提示");
}
protected function button3_clickHandler(event:MouseEvent):void
{
// 删除
fileList.removeItem(this.file);
manualCheck.group.removeElement(this);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:TextInput id="fileName" width="350"/>
<mx:Button label="浏览" click="button2_clickHandler(event)" fontWeight="bold"
overSkin="@Embed(source='/assets/dfpBtn/btnliulan2.png')"
skin="@Embed(source='/assets/dfpBtn/btnliulan.png')"/>
<mx:Button click="button3_clickHandler(event)"
overSkin="@Embed(source='/assets/dfpBtn/deletebtn2.png')"
skin="@Embed(source='/assets/dfpBtn/deletebtn.png')"/>
</s:HGroup>
as:
var file:FileReference = fileList.getItemAt(i) as FileReference;
var request:URLRequest=new URLRequest("s/upload/uploadFile");
request.data=new URLVariables();
request.data.orderRedoRecord=n;
try{
file.upload(request,"file");
} catch (error:Error){
isSuccess = false;
Alert.show("文件上传失败");
}
后台java:
@RequestMapping(value = "/uploadFile")
@ResponseBody()
public String UploadFiles(@RequestParam(value = "file") MultipartFile file) {
String result = "";
if (!file.isEmpty()) {
try {
String attachName = file.getOriginalFilename();
logger.info(attachName);
String filePath = "你的路径";
//此处省去业务代码
//FileUtils.saveDataToFile(file.getBytes(), filePath);
result = "上传成功";
} catch (Exception e) {
// TODO Auto-generated catch block
result = "上传失败";
e.printStackTrace();
}
}
return result;
}