Skip to content

doc: added documentation for input type=file #218

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions content/docs/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,61 @@ Overall, this makes it so that `<input type="text">`, `<textarea>`, and `<select
><select multiple={true} value={['B', 'C']}>
>```

## The file input Tag

In HTML, a `<input type="file">` tag lets the user choose one or more files from their devices storage, allowing the app to handle them to accomplish it's goals.

```html
<input type="file">
```

In React a `<input type="file">` works similarly to a normal `<input/>` with some major differences. This input is read-only so programmatically setting the value is impossible.
You can use the various File management API provided by Javascript to handle the files that are uploaded by the user.

```javascript{5,12-18,31}
class FileForm extends React.Component {
constructor(props) {
super(props);
this.state = {
file: ''
};

this.handleFileChange = this.handleFileChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleFileChange(e) {
e.preventDefault();

this.setState({
file: e.target.files[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think the file needs to be saved in state, unless I'm overlooking something. Probably having a ref to the file input and use it to access the file(s) in submit handler (or wherever required) would be a better option IMHO. See if this makes sense - https://codepen.io/tsriram/pen/qVNoZL?editors=0011

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, using the file by ref is definitly a betetr solution. My bad. Will change it ASAP

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Friendly ping 😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry guys, been away for a while and didn't notice the progress of the issue. Seems like @vishalvrv9 has worked on a fix of my PR based on @tsriram's suggestion. Should I go ahead and fix mine or would it better to merge his?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't reviewed his PR yet, so I don't know for sure. But since he created his when this one seemed to have been dropped, maybe we should try his?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, had some issues so I couldn't track down my PR after submitting it but the man definitly deserves some attention for his efforts ;)

});
}

handleSubmit(e) {
if (this.state.file.size/1024/1024 > 100) alert('The file is too large');
else alert('The ' + this.state.file.name + ' file is acceptable.');
e.preventDefault();
}

render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Pick a file (100MB max):
<input type="file" onChange={this.handleFileChange} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
}
```

[Try it on CodePen.](https://codepen.io/el1flem/pen/qVBrRv?editors=0011)

The `file input` accepts any props that the HTML accepts, such as `multiple` to provide support for multiple file selection and so on.

## Handling Multiple Inputs

When you need to handle multiple controlled `input` elements, you can add a `name` attribute to each element and let the handler function choose what to do based on the value of `event.target.name`.
Expand Down