表单数据以及使用Ajax和spring上传文件

时间:2022-12-07 20:06:58

i am using spring , Ajax and trying to upload file along with other data

我使用spring,Ajax并尝试上传文件和其他数据

 if(form.field_UploadFile != undefined){
        formContent.field_UploadFile=$( form.field_UploadFile  )[0].files[0];
        }
   var fields ={};
   for(var i=0; i<form.elements.length; i++){
       if (form.elements[i].name){
       if(form.elements[i].name.substring(0,6)=="field-"){
           if(form.elements[i].type=='checkbox'){
              if(form.elements[i].checked){
                   fields[form.elements[i].name]='checked';
               }else{
                   fields[form.elements[i].name]='unchecked';
               }
           }else{
                fields[form.elements[i].name]=form.elements[i].value;
           }
       }
       }
   }

   $.ajax({  
       type: "POST",  
       url: "${pageContext.request.contextPath }/forms/createnocaptcha",
       data: formContent,  
       dataType: "json",  
       contentType: false, 
       processData: false,

       complete: function (xhr, status) {
            $('html, body').animate({ scrollTop: 0 }, 0);              
            if (status === 'error' || !xhr.responseText) {
                //alert("error");
                $("#" + messagedivid).addClass("errorMessage");
                $("#" + messagedivid).html("Form sunewsbmission error");
            }
            else {
                var data = xhr.responseText;
                //$("#" + messagedivid).addClass("successMessage");
                //$("#" + messagedivid).html(data);
                $("#" + feedbackdivid).addClass("successMessage");
            $("#" + feedbackdivid).show();
            $("#" + messagedivid).hide();
            $(form)[0].reset();
            }

controller

@RequestMapping(value = "/forms/createnocaptcha", method=RequestMethod.POST)
@ResponseBody
public String createPageNoCaptcha( Form formContent, HttpServletRequest request, HttpSession session){
    boolean status = false;

If i put requestBody for formContent then i get 415 unsupported media and if it is removed then the form values are null.

如果我为formContent放置requestBody,那么我得到415不支持的媒体,如果它被删除,那么表单值为空。

And also can I use same controller for both Multi part and non multipart.

我也可以为多部分和非多部分使用相同的控制器。

Please advise

Thanks

1 个解决方案

#1


0  

We need to convert js object to JSON string before sending it.

我们需要在发送之前将js对象转换为JSON字符串。

Change your data in ajax call like this,

在这样的ajax调用中更改数据,

data: JSON.stringify(formContent), 

and then use @RequestBody Form formContent And change contentType in Ajax call to application/json

然后使用@RequestBody Form formContent并在Ajax调用application / json中更改contentType

#1


0  

We need to convert js object to JSON string before sending it.

我们需要在发送之前将js对象转换为JSON字符串。

Change your data in ajax call like this,

在这样的ajax调用中更改数据,

data: JSON.stringify(formContent), 

and then use @RequestBody Form formContent And change contentType in Ajax call to application/json

然后使用@RequestBody Form formContent并在Ajax调用application / json中更改contentType