Skip to content
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

HTML5 File Upload API #79

Open
Teelevision opened this issue Nov 30, 2016 · 2 comments
Open

HTML5 File Upload API #79

Teelevision opened this issue Nov 30, 2016 · 2 comments

Comments

@Teelevision
Copy link

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:

this.setState({
  value : e.target.files[0]
}, this.props.onChange.bind(null, e.target.files[0]));

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

@andrewhathaway
Copy link
Owner

Hi @Teelevision.

You're completely right, and this should be the case. Bit of an oversight on my part as I will admit I just chucked that input type together.

I imagine that if I set the value to e.target.files being a FileList and not a File, this should work out?

Let me know your thoughts!

@Teelevision
Copy link
Author

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.

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

No branches or pull requests

2 participants