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" />
Nico Griffioen
  • 5,143
  • 2
  • 27
  • 36
yaqub
  • 63
  • 2
  • 9
  • possible duplicate of https://stackoverflow.com/questions/10105411/how-to-limit-the-maximum-files-chosen-when-using-multiple-file-input – Harish Nov 12 '19 at 07:05

1 Answers1

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;
  }
}
junwen-k
  • 3,454
  • 1
  • 15
  • 28