react-file-base64 icon indicating copy to clipboard operation
react-file-base64 copied to clipboard

How to Reset Upload and Preview

Open Firiyuu opened this issue 4 years ago • 2 comments

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?

Firiyuu avatar Oct 22 '20 11:10 Firiyuu

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'/> ); } }

farhan-hafizh avatar May 18 '22 05:05 farhan-hafizh

Estou com mesmo problema. Não consegui entender a solução acima.

Gabriel-Mariano avatar Jan 11 '23 23:01 Gabriel-Mariano