weixin_40119478
Zaylour
2018-04-10 08:31
采纳率: 69.2%
浏览 1.0k
已采纳

springmvc+ajax上传图片的问题。传过去的是空值,这是为什么?

html代码



邮箱*



账号*



密码*



昵称*



介绍*



头像*







-----------------------------------------------------------------------------------
js代码:
<br> $(function () {<br> $(&quot;#btn&quot;).click(function () {</p> <pre><code> var f = $(&quot;#file&quot;).val(); if(f==null||f==&quot;&quot;){ $(&quot;#picTip&quot;).html(&quot;&lt;span style=&#39;color:Red&#39;&gt;错误提示:上传文件不能为空,请重新选择文件&lt;/span&gt;&quot;); }else{ var extname = f.substring(f.lastIndexOf(&quot;.&quot;)+1,f.length); extname = extname.toLowerCase();//处理了大小写 if(extname!= &quot;jpeg&quot;&amp;&amp;extname!= &quot;jpg&quot;&amp;&amp;extname!= &quot;gif&quot;&amp;&amp;extname!= &quot;png&quot;){ $(&quot;#picTip&quot;).html(&quot;&lt;span style=&#39;color:Red&#39;&gt;错误提示:格式不正确,支持的图片格式为:JPEG、GIF、PNG!&lt;/span&gt;&quot;); } } adduser(); }); }); function adduser(){ var email=$(&quot;#email&quot;).val(); var account=$(&quot;#account&quot;).val(); var password=$(&quot;#password&quot;).val(); var nickname=$(&quot;#nickname&quot;).val(); var introduce=$(&quot;#introduce&quot;).val(); var formData = new FormData($(&quot;#uploadForm&quot;)[0]); alert(formData) alert(email) $.ajax({ url:&quot;./zhuce&quot;, data:{&quot;email&quot;:email,&quot;account&quot;:account,&quot;password&quot;:password,&quot;nickname&quot;:nickname,&quot;introduce&quot;:introduce,&quot;formData&quot;:formData}, type:&quot;post&quot;, async: false, cache: false, contentType: false, processData: false, success: function (data) { if(data==&quot;error&quot;){ alert(&quot;注册失败!&quot;) } if(data==&quot;success&quot;){ alert(&quot;成功!&quot;) window.location.href=&quot;login.jsp&quot; } } }); } &lt;/script&gt; ----------------------------------------------------------------------- </code></pre> <p>后台controller代码:<br> @RequestMapping(value=&quot;/zhuce&quot;)<br> @ResponseBody<br> public String zhuce(String email,String account,String password,String nickname,String introduce,@RequestParam(value=&quot;file&quot;,required = false) CommonsMultipartFile file,HttpServletRequest request, Model model){<br> System.out.println(&quot;邮箱和账号&quot;+email+&quot;,&quot;+account);<br> System.out.println(&quot;hhhh&quot;);<br> System.out.println(&quot;filename:&quot;+file.getOriginalFilename());</p> <pre><code> String path = request.getSession().getServletContext().getContextPath(); System.out.println(&quot;path:&quot;+path); return &quot;success&quot;; } ---------------------------------------------------------------- </code></pre> <p>运行结果是:<br> 邮箱和账号null,null<br> hhhh<br> java.lang.NullPointerException--获取文件名字这里是空的</p>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • baizzj
    baizzj 2018-04-10 09:17
    已采纳

    var _formData = new FormData($("#tztg_saveForm")[0]);
    $.ajax({
    url: '',
    type: 'POST',
    cache: false,
    data: _formData,
    processData: false,
    contentType: false,
    dataType: "json",
    }).done(function (data) {
    }).fail(function (e) {
    return false;
    });
    这是我做的一个项目做的上传。你可以参考下

    点赞 评论
  • qq_24225065
    qq_24225065 2018-04-10 08:48

    要不就用js的插件 要不就自己写java接受文件流来接受

    点赞 评论
  • qq_27130997
    安小然然 2018-04-11 04:48

    ajax的data数据类型可以是_formData吗

    点赞 评论

相关推荐