使用jquery.form.js进行上传
Spring mvc进行上传操作,原本简单的form表单用submit提交就可以,但是需要有回调。这个方式就无法满足了,在网上找了找可以用jquery.form.js进行上传操作,下面看代码。
表单部分
<form id="excel_form" name="excel_form" method="post" action="${appName}/links/springUpload.do" enctype="multipart/form-data">
<div class="addNewUser">
<table width="90%" align="center">
<tr>
<td align="center" bgcolor="#eeeeee">选择文件</td>
<td bgcolor="#ffffff"><input type="file" id="excel_up" name="excel_up"/></td>
</tr>
</table>
<a href="javascript:void(0);" class="btn_mFormTable_save" id="sub" name="sub" onclick="submit()">上传导入</a>
</div>
<div id="msg_up" style='text-align: center;font-size: 20px;'></div>
</form>
提交部分
<script type="text/javascript" src="${appName}/common/javascript/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="${appName}/common/javascript/layer.js"></script>
<script type="text/javascript" src="${appName}/links/js/jquery.form.js"></script>
<script type="text/javascript">
function submit() {
$('#msg_up').html("<strong><embed id='xunlei_com_thunder_helper_plugin_d462f475-c18e-46be-bd10-327458d045bd' type='application/thunder_download_plugin' height='0' width='0'/><span style='color: rgb(255, 0, 0);'>导入中,请稍后……</span></strong>");
$("#sub").removeAttr("onclick");
var file = $('#excel_up').val();
if (file.length > 0) {
var options = {
url: '${appName}/links/springUpload.do',
type: 'POST',
success: function (data) {
alert(data.msg);
parent.selectPage(0);
var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
parent.layer.close(index);
}
};
$('#excel_form').ajaxSubmit(options);
return false;
}
}
</script>