关于jquery异步上传文件出错的问题

来源:互联网 发布:java项目代码例子 编辑:程序博客网 时间:2024/06/03 18:13

   前段时间在开发过程中用到了文件上传,项目采用框架为ssi框架。上传文件的时候在form表单中也添加了enctype="multipart/form-data" 采用二进制上传,由于采用了异步,用了jquery中的$.ajax()上传,

$.ajax({
type : "POST",
url : ctx + "/auth/college/colCompetPrize/saveColCompetPrize.htm",
dataType : "json",
data:$("#formEdit").serialize(),
beforeSend:function(){
$_this.addClass('u-btn-gray');
$_this.val('保存中...');
},
success : function(json) {
if (json.success) {
Utils.Notice.alert(json.message);
setTimeout(function(){
Dialog.submit('colCompetPrize-edit','submit');
},700);
} else {
Utils.Notice.error(json.message);
}
},
complete:function(){
$_this.val('保存');
$_this.removeClass('u-btn-gray');
$_this.attr('isC','0');
}
});

注意其中的form表单的数据是利用data:$("#formEdit").serialize(),数据是用form表单序列化,然后提交的。这种方式造成了后台无法接收文件的数据。

后来发现了以后,用到了ajaxForm提交,解决了此问题。

$('#formEdit').ajaxSubmit({
dataType : "json",
beforeSend:function(){
$_this.addClass('u-btn-gray');
$_this.val('保存中...');
},
success : function(data) {
if(data.response_result == 100) {
alert("操作成功!"); 
setTimeout(function(){
window.top.location.reload();
},700);
}else if (data.reponse_result == -100) {
alert("操作失败!");
}
},
complete:function(){
$_this.val('保存');
$_this.removeClass('u-btn-gray');
$_this.attr('isC','0');
}
});


0 0
原创粉丝点击