0

This is the jsp code,

Username: <input type="text" name="user_name"/>
File: <input type="file" name="profile_img" />
<input type="submit" value="Save" onclick="saveProfileData()"/>

And this is the javscript code,

function saveProfileData() {
    var user_name = document.getElementById("user_name").value;
    var profile_img = document.getElementById("profile_img").value;
    $.ajax({ 
        type: "POST",  
        url: /login/saveProfileData,  
        data:{ 
            "user_name": user_name,
            "profile_img":profile_img
        },
        success: function(response){
            //other code
        },
        error: function(e){
            //alert('Error: ' + e);
         }
        });
    }

@ResponseBody
@RequestMapping(value="/saveProfileData", method=RequestMethod.POST)
public int saveProfileData(@RequestParam(required=false) String user_name, MultipartFile profile_img) { 
    System.out.println(user_name); 
    //int i = code for save profile data.
    return i;
}

When I click on save button, It gives this error The current request is not a multipart request. Why this is happening and how to fix this? How can I send the values with image?

Please anyone help me.?

rockroyal
  • 25
  • 7

1 Answers1

0

Have you tried adding the encodingtype of the input field for the file, like such:

File: <input type="file" name="profile_img" enctype = "multipart/form-data"/>
Paaz
  • 133
  • 7
  • Try taking a look at this stackoverflow post: https://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet – Paaz Oct 04 '17 at 06:39