extjs 上传文件到数据库

效果图:

在弹出的增加框中,点击浏览按钮,选择文件打开,然后在加载一个上传的浏览,点击增加文件之后上传文件到数据库中,

jsp页面:

var addProjectDetailsField = Ext.create("Ext.form.FieldSet", {
	height :  '100%',
	width : 700,
	border:false,
	layout : "column",
	id : 'addProjectDetailsField',
	items : [{
		xtype : 'filefield',
		columnWidth : 0.5,
		labelWidth : 65,
		margin : '10 20 10 0',
		fieldLabel : '合同附件',
		name : 'attachment',
		// allowBlank:false,
		buttonText : '浏览..',
		isFirst : true,
		listeners : {
			change : uploadMore
		}
	},{
		xtype : 'filefield',
		columnWidth : 0.5,
		labelWidth : 65,
		margin : '10 20 10 0',
		fieldLabel : '资质附件',
		name : 'qualificationAttachment',
		// allowBlank:false,
		buttonText : '浏览..',
		isFirst : true,
		listeners : {
			change : uploadMore1
		}
	}]
});
新增一个上传:

function uploadMore() {
	var upload = Ext.create('Ext.form.field.File', {
		columnWidth : 0.5,
		labelWidth : 65,
		margin : '10 20 10 0',
		fieldLabel : '合同附件',
		name : 'attachment',
		buttonText : '浏览..',
		isFirst : true,
		listeners : {
			change : uploadMore
		}
	});

	if (this.isFirst) {
		var formpanel = Ext.getCmp("addProjectDetailsField");
		formpanel.insert(12, upload);
		formpanel.doLayout();
		this.isFirst = false;
	}
}
在发送ajax请求的时候,上传内容不需要带参数,在action层处理

action层:

	public String addSupplierManagement(){
		String seq = service.addSupplierManagement(suppliermnt);
		
		HttpServletRequest req = ServletActionContext.getRequest();
		MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) req;
		
		File[] files = wrapper.getFiles("attachment");
		String[] filenames = wrapper.getFileNames("attachment");
		
		Attachmentuploadfile cf = null;
		List<Attachmentuploadfile> liUp = new ArrayList<Attachmentuploadfile>();
		InputStream in =null;
		byte[] b=null;
		
		try {
			for (int i = 0; i < files.length; i++) {
				in= new FileInputStream(files[i]);
				b=new byte[(int)files[i].length()];
				for (int j = 0; j < b.length; j++) {
					b[j]=(byte) in.read();
				}
				
				if (in != null) {
	                try {
	                    in.close();
	                } catch (Exception e) {
	                    e.printStackTrace();
	                }
				}
				
				cf =new Attachmentuploadfile();
				cf.setProjectid(seq);
				cf.setAttachmentname(new String(filenames[i].getBytes("gbk")));
				cf.setAttachment(b);
				liUp.add(cf);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
			
		Attachmentuploadfile cu = null;
		for (int i = 0; i < liUp.size(); i++) {
			cu= liUp.get(i);
			service.saveAttachmentUploadFile(cu);
		}
		
		StringBuilder json = new StringBuilder("{success:true}");
		this.setJsonStr(json.toString());
		return SUCCESS;
	}
	

OK,这样就上传到数据库中了,下一篇 :extjs 页面显示上传文件名,并点击下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值