You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the disableSubmit property one can use the form data however one likes, for example sending it to some server in the background. This does not work with file uploads. The value of the file input is something like "C:\fakepath\lena512.bmp" since the browser does not reveal the real path of the file. That value is useless for uploading the file. But fortunately HTML5 provides the File Upload API which can help. The files of the file input can be easily retrieved via target.files, which returns a FileList object.
How to implement this
Changing the handleChange() method of the FileInput component like so:
While this works in my case where I have a single file, it probably will not work with multiple files, since files[0] retrieves only the first file.
Why I think this should be implemented
In the scope of a single page application built using React this library could be more relevant if it would provide developers the means of handling files in a modern way, the HTML5 way.
FileList should work. The question might also be: when will the developer expect a FileList and when a File? Or is it reasonable to always return a FileList, even if it is a single file input?
In another project I was faced with this decision and decided to provide separated input types for single and multiple file inputs. The benefit was that it was clear what to expect as an input. The downside was that the users needed to distinguish between both types while semantically both are pretty much just file inputs.
In this case I would probably stick to the way it is handled in the browser: always use the FileList, never use the single File like I did above. At least developers will know the concept.
When using the
disableSubmit
property one can use the form data however one likes, for example sending it to some server in the background. This does not work with file uploads. Thevalue
of the file input is something like"C:\fakepath\lena512.bmp"
since the browser does not reveal the real path of the file. That value is useless for uploading the file. But fortunately HTML5 provides the File Upload API which can help. The files of the file input can be easily retrieved viatarget.files
, which returns aFileList
object.How to implement this
Changing the
handleChange()
method of theFileInput
component like so:While this works in my case where I have a single file, it probably will not work with multiple files, since
files[0]
retrieves only the first file.Why I think this should be implemented
In the scope of a single page application built using React this library could be more relevant if it would provide developers the means of handling files in a modern way, the HTML5 way.
More on the API
https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications
The text was updated successfully, but these errors were encountered: