As the title says, I need to get the real filesize of a file posted as a multipart/form-data
request.
I'm based in the JSF world but I think the problem is technology-agnostic (only depends on HTTP).
The value specified in the Content-Length header is not sufficient because it includes the boundaries and other form parameters.
Example request (body only):
-----------------------------355432893771247592819421210
Content-Disposition: form-data; name="composeform"
composeform
-----------------------------355432893771247592819421210
Content-Disposition: form-data; name="javax.faces.ViewState"
-9107117821100047188:3613431652649065231
-----------------------------355432893771247592819421210
Content-Disposition: form-data; name="file"; filename="3byte.txt"
Content-Type: text/plain
abc
-----------------------------355432893771247592819421210--
The last part of the request body contains the file, in this case a file named "3byte.txt" containing the characters "abc".
As you can see, the filesize is not included in the request, therefore the only way I see to get the value, is to calculate it based on the Content-Length header and the size of the boundaries and other form parameters.
Additional requirements:
- the solution shouldn't have to read/parse the whole request to get the filesize (this would obviously be easy)
- there may be more form parameters in the request, before or/and after the part which contains the file (in contrast to my example)
Similar questions not solving the problem:
- How to know the file size when uploading via multipart form-data?
- Get file size from multipart form upload Content-Length header
- Calculate size multipart/form-data encoded file