Ext多文件上传下载

以下代码为项目应用中整理,仅供参考。

 

一.多文件上传

 

   1.前台Js代码事例 

items : [
	{
		xtype : 'button',
		text : '添加附件',
		iconCls : 'silk_page_add',
		handler : function() {
			Ext.getCmp("upfile").addFile();
		}
	}, {
		xtype : 'multifileupload',
		width : 750,
		allowBlank : false,
		fileConfig : {
			xtype : 'fileuploadfield',
			emptyText : '选择文件上传',
			fieldLabel : '上传文件',
			anchor : '95%',
			buttonCfg : {
				text : '',
				iconCls : 'silk_folder_find'
			}
		},
		id : 'upfile',
		startId : 'post',
		startName : 'post',
		labelWidth : 70,
		limit : 10,
		allowBlank : false
	}
]

 2.后台代码获取上传附件

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
Set<MultipartFile> fileset = new LinkedHashSet<MultipartFile>();
for (Iterator it = multipartRequest.getFileNames(); it.hasNext();) {
	String key = (String) it.next();
	MultipartFile file = multipartRequest.getFile(key);
	if (file.getOriginalFilename().length() > 0) {
		fileset.add(file);
	}
}

 注:多附件时可考虑专门建立一个附件表存放附件,在表单提交时需更新附件表,
      同时将多个附件的主键ID以”,”隔开存到主表中。

 

二.多文件展示,下载,删除

 

1.前台Js代码,创建fieldSet

function createDownLoadFieldSet(data,messageId) {
	downLoadFieldSet = new Ext.form.FieldSet({
				title : '附件下载',
				border : true,
				width : '100%',
				layout : 'column',
				autoHeight : true,
				collapsible: true,
				collapsed : true
			});
	// data为附件表的主键ID,多个以;隔开
	var attachUrls = data;
	var attachaUrlArr = attachUrls.split(";");
	attachaUrlArr.pop();
	Ext.each(attachaUrlArr, function(urlid) {
		callBackUrl(urlid,messageId);
	})
}

 2.Js附件展示代码 

 

function callBackUrl(urlid,messageId){
	Ext.Ajax.request({
		url : SMIS.CTX+'/work/post/postListController/getDocumentByUuid.do',
		method : 'POST',
		params : {
			urlId : urlid
		},
		success : function(resp,opt) {
			var doc = Ext.util.JSON.decode(resp.responseText);
			// 附件路径
			var url = doc.attachment
			var pathArr = url.split("/");
			var fileName = pathArr[pathArr.length - 1];
			var temp = downLoadFieldSet.add({
				columnWidth : 1,
				id : url,
				style : {
					padding : '0 0 5 0'
				},
				layout : 'column',
				items : [{
					xtype : 'label',
					width : '45%',
					text : fileName.substring(fileName.
							indexOf("_")+1,fileName.length),
					iconCls : 'silk_application_view_gallery'
				}, {
					xtype : 'label',
					width : '5%'
				}, {
					xtype : 'label',
					width : '25%',
					text : new Date(parseInt(fileName.substring(0,
									fileName.indexOf("_")))).toLocaleString(),
					iconCls : 'silk_application_view_gallery'
				}, {
					xtype : 'button',
					text : '下载附件',
					iconCls : 'silk_application_put',
					handler : function() {
						// Js方法,将URL中的中文进行编码
						url = encodeURI(url); 
						window.open(SMIS.CTX + '/work/post/postListController
									/getAttachment.do?attachmentFile='+ url);
						url = decodeURI(url);
					}
				}, {
					xtype : 'button',
					text : '删除附件',
					iconCls : 'silk_application_put',
					handler : function(){
						removeAttachment(temp,messageId,url);
					}
				}]
			})
		},
		failure : function(){
			Ext.Msg.alert('执行失败',"执行失败!");
		}
	})
}

 

 3.附件移除代码

 

 

function removeAttachment(temp,messageId,url){
	u = encodeURI(url); 
	Ext.Ajax.request({
		url : SMIS.CTX + '/discipline/mechanism/SmisInstitutionController
		/deleteAttachment.do?messageId='+messageId+'&attachmentFile='+u,
		method : 'POST',
		success : function(resp ,action){
			if(Ext.util.JSON.decode(resp.responseText).success){
			Ext.Msg.alert("执行成功",Ext.util.JSON.decode(resp.responseText).msg);
			var newattachments = Ext.getCmp("attachments").getValue()
				.replace(Ext.util.JSON.decode(resp.responseText).documentId,"");
			Ext.getCmp("attachments").setValue(newattachments);
				// 移除附件显示
				downLoadFieldSet.remove(Ext.getCmp(url));
			}else{
			Ext.Msg.alert("执行失败",Ext.util.JSON.decode(resp.responseText).msg);
			}
		},
		failure : function(){
			Ext.Msg.alert("执行失败","执行过程中发生错误!");
		}
	})	
}

 

4.后台代码获取附件进行下载

java.net.URLDecoder.decode(request.getParameter("attachmentFile"),"UTF-8"); 
String attachmentFile = java.net.URLDecoder.decode(
							request.getParameter("attachmentFile"),"UTF-8"); 
response.setContentType("text/html;charset=UTF-8");
try {
	File file = new File(attachmentFile);
	InputStream fis = new BufferedInputStream(new FileInputStream(file));
	byte[] buffer = new byte[fis.available()];
	fis.read(buffer);
	fis.close();
	response.reset();
	response.addHeader("Content-disposition", "attachment;filename=\"" 
			+ new String(file.getName().getBytes("gb2312"), "ISO8859-1"));
	response.addHeader("Content-Length", "" + file.length());
	OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
	toClient.write(buffer);
	toClient.flush();
	toClient.close();
} catch (IOException ex) {
	ex.printStackTrace();
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在EXT.js中实现上传文件功能,可以使用Ext.form.Panel和Ext.form.field.File组件。具体步骤如下: 1. 创建一个Ext.form.Panel组件,设置属性和方法。 ```javascript var formPanel = Ext.create('Ext.form.Panel', { renderTo: Ext.getBody(), width: 400, height: 150, title: '上传文件', bodyPadding: 10, frame: true, url: 'upload.php', //上传文件处理的URL method: 'POST', //上传文件的方式 items: [{ xtype: 'filefield', //上传文件的组件 name: 'file', //上传文件的参数名 fieldLabel: '选择文件', labelWidth: 70, msgTarget: 'side', allowBlank: false, buttonText: '浏览...' }], buttons: [{ text: '上传', handler: function() { var form = this.up('form').getForm(); if (form.isValid()) { form.submit({ success: function(form, action) { Ext.Msg.alert('成功', action.result.msg); }, failure: function(form, action) { Ext.Msg.alert('失败', action.result.msg); } }); } } }] }); ``` 2. 创建一个Ext.form.field.File组件,用于选择要上传的文件。 ```javascript { xtype: 'filefield', name: 'file', fieldLabel: '选择文件', labelWidth: 70, msgTarget: 'side', //错误提示的位置 allowBlank: false, //是否允许为空 buttonText: '浏览...' //选择文件按钮的文本 } ``` 3. 在表单中添加按钮,用于提交表单数据。 ```javascript { xtype: 'button', text: '上传', handler: function() { var form = this.up('form').getForm(); if (form.isValid()) { //验证表单是否合法 form.submit({ //提交表单数据 success: function(form, action) { Ext.Msg.alert('成功', action.result.msg); }, failure: function(form, action) { Ext.Msg.alert('失败', action.result.msg); } }); } } } ``` 4. 在服务端处理上传文件。 在上面的代码中,上传文件处理的URL为'upload.php',需要在该文件中处理上传的文件,将文件保存到服务器上。具体代码如下: ```php <?php $targetDir = "uploads/"; //上传文件保存的目录 $targetFile = $targetDir . basename($_FILES["file"]["name"]); //上传文件的路径 $uploadOk = 1; $imageFileType = strtolower(pathinfo($targetFile,PATHINFO_EXTENSION)); //文件类型 //检查文件是否已经存在 if (file_exists($targetFile)) { echo json_encode(array('msg' => '文件已经存在')); $uploadOk = 0; } //检查文件大小 if ($_FILES["file"]["size"] > 5000000) { // 5MB echo json_encode(array('msg' => '文件太大')); $uploadOk = 0; } //检查文件类型 if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo json_encode(array('msg' => '只允许上传jpg、png、jpeg和gif格式的文件')); $uploadOk = 0; } //上传文件 if ($uploadOk == 0) { echo json_encode(array('msg' => '上传失败')); } else { if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFile)) { echo json_encode(array('msg' => '文件上传成功')); } else { echo json_encode(array('msg' => '上传失败')); } } ?> ``` 以上就是在EXT.js中实现上传文件功能的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值