var html = "<form id='file_form' action='attence!upAttenceExcel.do' enctype='multipart/form-data'"
+ "method='post'>"
+ "<table width='300' class='tablesorter' border='0' " +
"cellpadding='2' cellspacing='1' align='center' >"
+ "<tr bgcolor='#ffffff' height='22'>"
+ "<td>"
+ "excel文件:"
+ "</td>"
+ "<td>"
+ "<input type='file' name='upload' id='file_input' />"
+ "</td>"
+ "<td>"
+ "<input type='hidden' name='moduleId' value='"+moduleid+"'/>"
+ "<input type='hidden' name='positionId' value='"+positionid+"'/>"
+ "<input type='hidden' name='teamroleId' value='"+teamroleid+"' />"
+ "</td>" + "</tr>" + "</table>" + "</form>"
art.dialog({
content : html,
ok : function() {
var fileName = $('#file_input').val();
if (fileName === '') {
alert('请选择文件');
return false;
}
var fileType = (fileName.substring(
fileName.lastIndexOf(".") + 1, fileName.length))
.toLowerCase();
if (fileType !== 'xls' && fileType !== 'xlsx') {
alert('文件格式不正确,请选择excel文件!');
return false;
}
$("#file_form").submit();
/* $("#file_form").ajaxSubmit({
dataType : "json",
success : function(data, textStatus) {
if (data['result'] === 'OK') {
console.log('上传文件成功');
} else {
console.log('文件格式错误');
}
return false;
}
}); */
}
});
}
/**
* 将上传的excel存到upload目录下
*/
this.struts2Util.getRequest().setCharacterEncoding("utf-8");
File saved = new File(ServletActionContext.getServletContext().getRealPath("upload"), uploadFileName);
InputStream ins = null;
OutputStream ous = null;
try {
saved.getParentFile().mkdirs(); // 确保文件夹upload存在
ins = new FileInputStream(upload); // 读取临时文件
ous = new FileOutputStream(saved); // 写入到upload下
byte[] b = new byte[1024];
int len = 0;
while ((len = ins.read(b)) != -1) {
ous.write(b, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (ous != null)
ous.close();
if (ins != null)
ins.close();
}