Ext文件上传事例

一.文件上传表单定义

 

		rms.custManage.custAbnormalApplyDetailUploadPanel = Ext.extend(Ext.FormPanel,{
			labelAlign : 'right',
			buttonAlign:'center',
			labelWidth : 65,
			height:80,
			frame : true,
			border : false,		
			layout : 'table',
			layoutConfig : {
				columns : 2
			},
			id:'custAbnormalApplyDetailUploadPanel',
			fileUpload : true,
			initComponent:function(){					
				this.items = [{
							layout : 'form',
							style : 'margin-top:5px;',
							items : [{
										xtype : 'textfield',
										fieldLabel : '附件区域',
										id : 'file',
										name : 'file',
										inputType : 'file',
										width : 300
									}]
						}, {
							layout : 'form',
							style : 'margin-left:10px;',
							items : [{
										xtype : 'button',
										text : '上传',
										width : 50,
										handler : uploadFile
									}]
						}, {
							colspan : 2,
							xtype : 'box',
							html : "<div id='docDiv'  style='font-size: " +
									"x-small;padding-left:70px;'>"+
									+ "</div>",
							autoHeight : true
						}];		
				// 初始化组件
				rms.custManage.custAbnormalApplyDetailUploadPanel
										.superclass.initComponent.call(this);
			}
		})

    注意点: 1.Ext.form.TextField 中设置 inputType:'file';
                   2.form里面的fileUpload设置为true;

 

二. 表单提交处理

 

	uploadForm.getForm().submit({
			method : 'post',
			url : ctx
					+ '/pages/custManage/custAbnormalUpload.page',
			waitMsg : '文件正在上传中...',
						success : function(f, action) {
			},
			failure : function() {
			}
		});

 

三. 后台控制处理

 

@RequestMapping("/pages/custManage/custAbnormalUpload.page")
public void uploadFile(HttpServletRequest request,
		HttpServletResponse response) throws Exception {	
	// 上传文件获取
	MultipartHttpServletRequest multipartRequest = 
							   (MultipartHttpServletRequest) request;
	CommonsMultipartFile file = (CommonsMultipartFile)
							   multipartRequest.getFile("file");	
    // 存放文件的绝对路径
	DataOutputStream out = new DataOutputStream(new FileOutputStream(filePath));
	InputStream is = null;// 附件输入流
	try{
		is = file.getInputStream();			
		byte[] buffer = new byte[1024];
		while (is.read(buffer) > 0) {
			out.write(buffer);// 写入磁盘;
		}
	}catch(Exception e){
		throw e;
	}finally{
		if (is != null) {					
			is.close();
		}
		if (out != null) {
			out.flush();
			out.close();
		}
	}
	return null;
}

 

  注意点:保存到数据库需保存其原始文件名和存放路径(存放路径的文件名以随机的时间为文件名)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值