react-file-base64
react-file-base64 copied to clipboard
How to Reset Upload and Preview
Hi I want to reset the upload file button on my form. I am using FileBase64 for it
<FileBase64
multiple={true}
onDone={this.getFiles1.bind(this)} />
What I tried was a button to set the state of the states involved.
<Button primary onClick={(e) => {
e.preventDefault();
this.setState({ files: [] })
this.setState({ fileValue: '' })
}}>Clear</Button>
But it wouldn't work. I also tried, this.files1.reset() but it got my an undefined property meaning reset() doesnt work on such an element. Have anybody encountered this library and how you have cleared the input?
I can do it this way
const [files, setFiles] = useState([]);
......
<FileBase multiple={true} style={{ display: 'none' }} id="contained-button-file" type='file' ref={fileReferences} onDone={(newFile)=> { setFiles([...files,...newFile]); } } />
To display:
{files.length>0? files.map((data, key) => { return ( displayUpload(data, key) ); } ):''}
The displayUpload:
const displayUpload = (data, key) => { if(imageExtensions.exec(data?.file?.name)){ const url = URL.createObjectURL(base64toImage(data)); return( <img key={key} style={{width: 50}} src={url} alt='Uploaded file'/> ); } }
Estou com mesmo problema. Não consegui entender a solução acima.