我在SSH2的项目中使用了jquery的ajaxfileupload.js进行无刷新的文件上传操作,但是我在后台输出接收到的File对象时,却输出了为null,是什么原因呢?后台根本连接收都没有接收到,下面我将贴出关键部分代码:
Html部分:
<fieldset style="margin-top:10px">
<legend>修改用户头像</legend>
<div id="id__form_modifyUserIcon" style="float:left;margin-left:20px">
<table><tr><td rowspan="2"><img src="resources/${userInfo.userIcon}" width="70" height="70" id="id_userIconImg" /></td>
<td><input type="file" name="id_userIcon" id="id_userIcon" onchange='$("#id_userIconImg").attr("src", $(this).val())' /></td></tr><tr><td align="right"><input type="button" value="确定修改" id="id_submitUserIcon" /></td></tr></table></div></fieldset>jquery部分:
         $("#id_submitUserIcon").click(function(event){
return ajaxFileUpload();
});
function ajaxFileUpload()   {           
        $("#loading")  
        .ajaxStart(function(){  
            $(this).show();  
        })//开始上传文件时显示一个图片  
        .ajaxComplete(function(){  
            $(this).hide();  
        });//文件上传完成将图片隐藏起来            
        $.ajaxFileUpload  
        (  {   url:'userCenter_changeMyIcon.action',//用于文件上传的服务器端请求地址  
                secureuri:false,//一般设置为false  
                fileElementId:'id_userIcon',//文件上传空间的id属性  <input type="file" id="file" name="file" />  
                dataType: 'json',//返回值类型 一般设置为json  
                success: function (data, status)  //服务器成功响应处理函数  
                {  //alert(data.message);//从服务器返回的json中取出message中的数据,其中message为在struts2中定义的成员变量  
                    //$("#uploadMeg").attr("src",data.message); 
             //var status = $.parseJSON(data);
             //var text = $.parseJSON(data);
             //alert(text.flag);
             //alert(data);
                 $("<div style='padding-top:8px;'>" + "<table>" +"<tr>" +
"<td>" +"<img src='resources/frontView/images/common/true.jpg' width='30' height='30' />" +"</td>" +"<td style='font-size:13px;color:green'>恭喜,头像上传修改成功~</td>" +"</tr>" +"</table>" +"</div>").floatingMessage({position : "left-top",time:3000});             
                },  error: function(msg)//服务器响应失败处理函数   { alert(msg.responseText);  }  }   )    return false;  } action部分:
public String changeMyIcon()
{
               //下面这句输出的是null,方法是进入了,但是值没有注进去
System.out.println("userIcon:" + getId_userIcon());
...
}struts.xml中的配置:
<package name="userCenter" namespace="/" extends="json-default">
<action name="userCenter_*" class="userCenterAction" method="{1}">
<result name="userCenter">/userCenter.jsp</result>
<result type="json" name="success">
<param name="contentType">text/html</param>
</result>
</action>
</package>

解决方案 »

  1.   

    同求,我用的是Servlet,也没有数据传过来
      

  2.   

    不管前台用什么方式,后台都一样,你却没把重要的后台action代码没发出来,怎么给你解决问题?struts2上传文件的时候对于属性的名字是有要求的比如前台<input type="file" name="id_userIcon"......
    这个文件域的名字为id_userIcon
    那么相对于userCenterAction中要有三个对应的属性去接收这个文件数据 private File id_userIcon;//这个是文件数据
    private String id_userIconContentType;//文件数型,如 image/png之类的
    private String id_userIconFileName;//文件名然后在changeMyIcon方法中把这个id_userIcon保存起来即可还有一个就是上专文件大小的设置
    struts.xml中
    <constant name="struts.multipart.maxSize" value="10485760"></constant>