Skip to main content
WolfShade
Brainiac
July 7, 2020
Answered

Does input type="file" exist in the submitted form scope if left blank?

  • July 7, 2020
  • 4 replies
  • 537 views

Hello, all,

 

I'm in the middle of troubleshooting an issue with server-side form validation, and am now curious.

 

If a form contains <input type="file" name="foo" /> but no file is selected when the form is submit, does the form element "foo" exist?  I mean, I know that if no checkboxes are checked, that element doesn't exist on form submit.  Do file inputs do the same?

 

V/r,

 

^ _ ^

This topic has been closed for replies.
Correct answer John123

Yes the form field for "foo" will be there but an empty string.

 

4 replies

BKBK
Community Expert
July 10, 2020

Hi WolfShade,

The following point is perhaps just a bit of craziness, but I shall put it out there. The file-upload form, together with content-type, is:

 

<form enctype="multipart/form-data">

<input type="file" name="foo" />

</form>

 

That is, the content-type is binary. Which gets me thinking that, when you submit the form without selecting a file, the form field exists, and has value null (clients then interpret the null as "").

 

This just might be relevant to a server whose validation code or response-header expects binary input. 

WolfShade
WolfShadeAuthor
Brainiac
July 7, 2020

Thank you, BeRadB and John.  I kind of suspected, but wasn't sure, and a Google search didn't help.

 

V/r,

 

^ _ ^ 

John123Correct answer
Participating Frequently
July 7, 2020

Yes the form field for "foo" will be there but an empty string.

 

BeRadB
Inspiring
July 7, 2020

I believe the form field will be there but the value will be empty.

Iambradb.com Adobe ColdFusion Specialist.