[v3 form-data-parser]: Handling file inputs with no selected files #10700
christoph-fricke
started this conversation in
Proposals
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Note
This discussion originated and has been moved over from mjackson/remix-the-web#59. I think it is still relevant to discuss.
Today, I have been surprised to learn that an empty file input results in a file in multipart/form-data content. I was super confused about receiving a file from
parseFormData
for an input where no file was selected.Turns out that the behavior is conform with the HTML specification (Section 4.10.21.4 "Constructing the entry list", Rule 5.8.1) and browsers are supposed to attach an empty file to the multipart/form-data payload for file inputs that contain no selected files.
This behavior can be easily verified with the following HTML snippet. Opening it in a browser (tested in Chromium) and submitting the form attaches an "empty file" in the multipart/form-data payload.
Using
form-data-parser
in a react-router action results in the following form data:Even though this behavior is according to the HTML specification, I am surprised to learn that
form-data-parser
does not filter empty file inputs. I did not find any hints or examples in the packages documentation that warn about this gotcha. They would all write this empty file to disk, right? Further, I did not find any tests inform-data-parser
ormultipart-parser
that verify the behavior around empty file inputs.I ended up filtering empty file inputs with this custom upload handler:
I am wondering whether or not
form-data-parser
should already filter empty file inputs for consumers. I think that it would greatly benefit the usability. If we want to be as close to the HTML standard as possible, the documentation should at least warn about this gotcha so users to not further process files that indicate an empty input. What are your thoughts about this topic?Beta Was this translation helpful? Give feedback.
All reactions