含有文件的form表单用jquery提交

来源:互联网 发布:一彩送货单软件 编辑:程序博客网 时间:2024/06/16 04:15

近期做项目遇到此问题,含有文件上传的form表单用ajaxSubmit提交,在此记录下,代码如下:


<form id="excelForm" name="excelForm" enctype="multipart/form-data" method="post">
<table>
<tr>
<td width="120" align="right" bgcolor="#f4f4f4">选择文件:<span style='color:red;'></span></td>
<td><input type="file" name="file" id="file"></td>
<td><input type="button" value="导入" onclick="excelImport()"></td>
</tr>
<tr>
<td width="120" align="right" bgcolor="#f4f4f4">示例模板:</td>
   <td bgcolor="white">
    <img height="20" with=" 30" src="/static/images/slmb.png"/><a href="javascript:location=encodeURI(encodeURI('/servlet/download.do?file_path=电话号码导入模板.xls&file_name=/hgj/hgjSms/telNo.xlsx'))">点击下载</a>
   </td>
</tr>
</table>
</form>


function excelImport(){

//校验是否选择文件
var file = $("#file").val();
if(file == null || file == ""){
alert("请选择文件!");
return false;
}

$("#excelForm").ajaxSubmit({
url : '/hgjSms/excelImport.htm',
type : 'POST',
success : function(data){
var obj = eval("("+data+")");
if(obj.resultType == "-1"){
alert("文件中以下行中的号码格式不正确:" + obj.resultStr);
return;
}else if(obj.resultType == "-2"){
alert("选择的文件未读到手机号码");
return;
}else if(obj.resultType == "-3"){
alert("系统异常");
return;
}else {
$("#checkCount").html(obj.memberCount);
          $("#sendCount").html(obj.memberCount);
          $("input[name='tel']").each(function(){
        $(this).attr('disabled',false);
    });
}
}
})
}

0 0
原创粉丝点击