前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flex上传文件

Flex上传文件

作者头像
全栈程序员站长
发布2022-07-07 20:59:29
3250
发布2022-07-07 20:59:29
举报

大家好,又见面了,我是全栈君。

前几天写了一篇jsp页面利用ajaxFileUpload上传文件。如今把flex上传页面也分享出来:

前台页面

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
@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;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116222.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档