Skip to content

Support Multiple Files for One Form Field in multipart/form-data #2872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dschulten opened this issue Mar 31, 2019 · 1 comment · Fixed by #2878
Closed

Support Multiple Files for One Form Field in multipart/form-data #2872

dschulten opened this issue Mar 31, 2019 · 1 comment · Fixed by #2878
Assignees
Milestone

Comments

@dschulten
Copy link

dschulten commented Mar 31, 2019

Affects Version(s): 5.1.3


Bug

The multipart/form-data RFC states:

multiple files MUST be sent
by supplying each file in a separate part but all with the same
"name" parameter

However, when I send a multipart message with three parts named data, only one makes it into the message.

Sample request (with Intellij's http client)
### Send a form with the text and file fields
POST http://localhost:8091/conversion
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="data.tiff"
Content-Type: image/tiff

< ../resources/inputSamples/data.tiff
--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="data2.tiff"
Content-Type: image/tiff

< ../resources/inputSamples/data2.tiff
--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="data3.tiff"
Content-Type: image/tiff

< ../resources/inputSamples/data3.tiff
--WebAppBoundary--

The reason is that MultipartAwareFormHttpMessageConverter#readMultipart uses multipartRequest.getFileMap()to access the files:

private MultiValueMap<String, ?> readMultipart(MultipartHttpInputMessage multipartRequest) throws IOException {
	MultiValueMap<String, Object> resultMap = new LinkedMultiValueMap<String, Object>();
	Map<?, ?> parameterMap = multipartRequest.getParameterMap();
	for (Entry<?, ?> entry : parameterMap.entrySet()) {
		resultMap.add((String) entry.getKey(), entry.getValue());
	}
	for (Map.Entry<String, MultipartFile> entry : multipartRequest
            .getFileMap().entrySet()) { //                               <== problematic
		MultipartFile multipartFile = entry.getValue();
		if (multipartFile.isEmpty()) {
			continue;
		}
		resultMap.add(entry.getKey(), this.multipartFileReader.readMultipartFile(multipartFile));
	}
	return resultMap;
}

which ultimately comes to AbstractMutlipartHttpServletRequest:

public Map<String, MultipartFile> getFileMap() {
    return getMultipartFiles().toSingleValueMap();
}

Here it loses all but the first files.

Possible solution: Use MultipartHttpInputMessage#getMultiFileMap instead of getFileMap. I suggest to treat this as a bug because it really seems that getFileMap was used inadvertently.

Workaround: use different names for all multipart files (pretty ugly, though).

@artembilan artembilan self-assigned this Apr 1, 2019
@artembilan artembilan added this to the 5.2.M1 milestone Apr 1, 2019
artembilan added a commit to artembilan/spring-integration that referenced this issue Apr 1, 2019
Fixes spring-projects#2872

The same HTML form entry may have several files in the multi-part request.
Parse all of them in the `MultipartAwareFormHttpMessageConverter.java`
 and re-map to the result `MultiValueMap`

**Cherry-pick to 5.1.x**
garyrussell pushed a commit that referenced this issue Apr 2, 2019
* GH-2872: Parse all the multi-part files

Fixes #2872

The same HTML form entry may have several files in the multi-part request.
Parse all of them in the `MultipartAwareFormHttpMessageConverter.java`
 and re-map to the result `MultiValueMap`

**Cherry-pick to 5.1.x**

* * Add test for multi-part files
garyrussell pushed a commit that referenced this issue Apr 2, 2019
* GH-2872: Parse all the multi-part files

Fixes #2872

The same HTML form entry may have several files in the multi-part request.
Parse all of them in the `MultipartAwareFormHttpMessageConverter.java`
 and re-map to the result `MultiValueMap`

**Cherry-pick to 5.1.x**

* * Add test for multi-part files
@dschulten
Copy link
Author

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants