I am creating a multipart form in React. Everything is working fine except for file input field.
Below is the code, what I am trying to do.
<div className="col-lg-6 col-md-12">
<form encType="multipart/form-data" onSubmit={this.onSubmit}>
<div className="form-group">
<div className="custom-file mb-3">
<input
type="file"
className="custom-file-input"
id="gerberFile"
onChange={this.handleFileChange}
/>
<label className="custom-file-label">
{gerberFileLabel}
</label>
</div>
{errors.gerberFile && (
<div className="alert alert-danger">
{errors.gerberFile}
</div>
)}
</div>
</div>
</div>
handleFileChange = e => {
const file = e.target.files[0];
if (!(file.name.includes(".zip") || file.name.includes(".rar"))) {
const errors = { ...this.state.errors };
errors.gerberFile = "Gerberfiles are only allowed in .zip or .rar";
this.setState({ errors });
} else {
console.log(`file ? ${JSON.stringify(file)}`);
const formFields = { ...this.state.formFields };
formFields.gerberFile = file;
this.setState({ formFields });
this.setState({ gerberFileLabel: file.name });
}
}
The strange part is I am able to console.log file name and use it as lable place holder. However, whenever I try to console.log File object I get an empty {} object.
What exactly I am doing wrong here? I went through multiple tutorials everyone seems to be doing the same thing and honestly i couldn't find it on SO.
I'd appreciate it greatly if anyone could help me.
name="file"property to your file input?onChange={e =>console.log(file: ${JSON.stringify(e.target.files[0])})}it is still logging an emtpy object