lundi 2 mars 2015

POST FormData object to Spring MVC

So I have a form made with AngularJS (doesn't fit the project at all but that's what the guy used) that has standard fields along with "file" type inputs. From what I've read it sounds like the FormData object would be a good way to save the form except that I can't get it to work at all. I tried following the methods here with no success, and I think the problem is I'm not writing the controller correctly. The actual form has many more fields and multiple files, but this is a simplified test I've been trying to get to work:


HTML:



<form enctype="multipart/form-data" method="post" name="testform">
<input type="file" name="file" required />
<input type="text" name="name"/>
</form>


JS:



function submit() {
var fd = new FormData(document.getElementById("testform"));
$.ajax({
url: saveUrl,
type: "POST",
data: fd,
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function (result) {...}
});
}


I've tried many variations of the controller using MultipartHttpServletRequest, an object modeling the form, different headers, etc. Nothing has really been close to working, but something vaguely like this:



@RequestMapping(value = "save", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
@ResponseBody
public String saveShipmentRequest(TestFormData testFormData) {
...
}


If someone could help that would be awesome. I'm also open to alternate methods of doing this, I barely know Angular and am fairly new to Spring, so maybe I'm taking the wrong approach all together...


Aucun commentaire:

Enregistrer un commentaire