vendredi 16 février 2018

upload image file to state with react

I am trying to upload an image with react with this code

<input type="file" id="InputFile" accept="image/*" value={this.state.image} onChange={this.handelChangeImage} />

this is the state

constructor(props) {
super(props);
this.state = {
    image: [],
}

this.handelChangeImage = this.handelChangeImage.bind(this);

}

and this is the binding function

handelChangeImage(event) {
   let image = event.target.files[0];
   let form = new FormData();
   form.append('image', image);
   this.setState({
       image: form,
   });

}

it gives me that error

Uncaught DOMException: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.

any help!!




Aucun commentaire:

Enregistrer un commentaire