0

I would like to share this piece of knowledge for the web devs out there, even though it's probably known:

If you're using the Spring framework and you want to accept a list of items as a multipart/form-data request, then Spring will only be able to correctly deserialize your JavaScript FileList in the backend if you have scripted it as follows:

var data = ev.dataTransfer.files;

var formData = formData();
for (i = 0, j = data.length; i < j; i++) {
formData.append('files', data[i]);
}

The for loop with the 'files' name is key here. Why? Because then it will resolve into:

key=val&key=val&key=val

and that's how Spring will correctly be able to deserialize it into a List. We remember from our HTML learnings that if we want values in a form to be processed as one, we must provide the same name= for each element in the form, otherwise if you have a separate name for each input, it won't be passed on as one collection of values.

This is why my list was originally null when received in the backend.

Courtesy of StackOverflow:

https://stackoverflow.com/questions...

Comments
Add Comment