日期:2014-05-16  浏览次数:20404 次

使用ExtJs的插件UploadDialog+struts2.0实现多文件上传
前一段时间项目需求要实现一个多文件上传的问题,项目框架为SSH,使用的是ExtJs的一个不是很成熟的插件UloadDialog上查了好多资料,发现好多都是copy有粘贴的内容,都很雷同,其中的一些问题还是要自己才找到答案,现在功能基本实现了,把我的代码贴出来供大家学习使用:
另外:咨询一个问题:使用UploadDialog能否实现一个选多个文件的?如果有哪位朋友实现了,帮忙贴出来供大家学习呀。
1.前台代码
在前台的html代码中只需要引入UploadDialog的包,在引入含下面代码的JS就可以使用了,很简单。
<!--当然,这里的路径要根据你自己放置的UploadDialog.js的路径来确定-->
<script type="text/javascript" src="${webRoot}/scriptLib/ext-3.2.1/Ext.ux.UploadDialog/UploadDialog/UploadDialog.js"></script>
<link rel="stylesheet" type="text/css" href="${webRoot}/ext-3.2.1/Ext.ux.UploadDialog/css/Ext.ux.UploadDialog.css"/>

2.upload.js
function btn_show(){
		var subject = Ext.getCmp('subjectId').getValue();
		var role = Ext.getCmp('roleId').getValue();
		var publicSound = Ext.getCmp('publicSoundId').getValue();
		
		if(subject==""||role==""){
			Ext.MessageBox.alert('提示信息', '请先选择音频科目和角色');
		}else{
			var dialog = new Ext.ux.UploadDialog.Dialog({
					        autoCreate			: true,
							minWidth			: 450,
							minHeight			: 300,
							fileSize			: 10*1024*1024,
					        title				: '文件上传',			//是否自动上传
							permitted_extensions: ['MP3','mp3','MP4','mp4','WMA','wma'],	//允许上传的文件的类型,区分大小写
							closable			: true,     
							collapsible			: false,     
							draggable			: true,       
							proxyDrag			: true,     
							resizable			: true,     
							constraintoviewport	: true, 
							modal				: true,
							reset_on_hide		: false,
							allow_close_on_upload: true,
							url					: 'uploadAudioHandelAction.action',
							base_params			:{
													subjectID		: subject,
													roleID			: role,
													publicSound		: publicSound,
													userName		: userName
								}
				});
			dialog.show('show-button');   
		    dialog.on('uploadsuccess',onUploadSuccess);//定义上传前的回调函数
			dialog.on('uploaderror',onUploadFailed); //定义上传出错回调函数
			dialog.on('fileadd',onFileAdd);//定义添加文件时验证文件的函数
			dialog.on('uploadcomplete',onUploadComplete);//定义所有文件上传完成回调函数
		}	
};

3.行处理的java代码:
package com.dfsoft.lion.process.demo.control.model.dismodel.audio;

import java.io.File;
import java.util.Date;


import com.dfsoft.lion.commons.EagleBaseAction;
import com.dfsoft.lion.domain.demo.measurement.entity.Audio;
import com.dfsoft.lion.domain.demo.measurement.entity.AudioSubjectInfo;
import com.dfsoft.lion.domain.demo.measurement.entity.Role;
import com.dfsoft.lion.domain.demo.measurement.entity.Users;
import com.dfsoft.lion.process.demo.process.model.dismodel.AudioProcess;
import com.dfsoft.lion.process.demo.process.model.dismodel.AudioSubjectInfoProcess;
import com.dfsoft.lion.process.demo.process.model.dismodel.RoleProcess;
import com.dfsoft.lion.process.demo.process.model.dismodel.UsersProcess;


import org.apache.commons.io.FileUtils; 
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;


/**
 * @描述:上传音频处理Action
 * @author lichenglin
 *
 */
public class UploadAudioHandelAction extends EagleBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7716496253903888292L;
	
	private Long subjectID ;//科目ID
	private Long roleID ;// 角色ID
	private int publicSound;
	private String userName ;
	private String filesPath;
	//接收文件信息
	private File[] files;  
	private String[] filesFileName;
	private String[] filesContentType;
	
	transient private AudioProcess audioProcess;
	transient private RoleProcess roleProcess;
	transient private AudioSubjectInfoProcess audioSubjectInfoProcess;
	transient private UsersProcess usersProcess;
	transient private GetPath getPath;
	
	public String execute() throws Exception{
		Audio audio = new Audio() ;
		Users users = this.usersProcess.getUserByName("userName", userName);
		AudioSubjectInfo audioSubjectInfo = new AudioSubjectInfo();
		
		HttpServletResponse response = ServletActionContext.getResponse();  
		respo