使用SpringMVC和js实现文件及字段上传中遇到的坑

来源:互联网 发布:windows光盘修复系统 编辑:程序博客网 时间:2024/06/06 01:40

界面代码:

<div class="easyui-layout" fit="true">
<form id="advertise_content_edit_form">
<table border="0" width="100%" cellpadding="5"  id="advertise_content_edit_table">
<tr>

                             <td><input type="file" id="test1" name="sharePic" /></td>

                              <td><input type="file" id="test2"  name-"userLog"/></td>

                            </tr>

                    </table>

          </form> 
</div>


后台代码:

class TestBean{

   private MultipartFile sharePic;

   private MultipartFile userLog;

}


上传用到的js

function sumbitData(){

   if($('#test1').val()==''){
$('#test1').removeAttr('name');
}else{
$('#test1').attr('name','sharePic');
}  

    if($('#test2').val()==''){
$('#test2').removeAttr('name');
}else{
$('#test2').attr('name','userLog');
} 

    

    var options={url:'<%=request.getContextPath()%>/advertisecontroller/editadvertisecontent.action?id=' + id,
beforeSubmit : function() {
return checkadvertisecontentformedit();//校验数据
},
type : 'POST',
success : function(result) {

}
};

$("#advertise_content_edit_form").ajaxSubmit(options);

     

}


这里特别要注意的是前面的判断file控制是否为空如果为空就去掉name属性,这是因为如果不去掉SpringMVC框架在读取到这个name属性

由于为空字符串强制转换为MultipartFile 会出现问题所有必须去掉这个name属性防止进行强制转换




阅读全文
0 0
原创粉丝点击