2

I want to limit the maximum upload of files while choosing a file in ReactJS. I'm not sure how to handle this. I tried with some solution but it did not work for me.

<input name="eventUpload" type="file" onChange={this.uploadMultipleFiles} multiple className="form-control-file" />
1

1 Answer 1

2

You can do it with some simple Javascript conditional check.

const MAX_LENGTH = 3;

uploadMultipleFiles = (e) => {
  if (Array.from(e.target.files).length > MAX_LENGTH) {
    e.preventDefault();
    alert(`Cannot upload files more than ${MAX_LENGTH}`);
    return;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.