Flex上传文件

10 篇文章 0 订阅

前几天写了一篇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;
}










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值