Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow input type=file to select the same file in react component

I have a react component which renders a <input type="file"> dom to allow user select images from browser. I found that its onChange method not called when I select the same file. After some searching, someone suggests using this.value=null or return false at the end of the onChange method but I have tried it doesn't work.

Below is my code:

<input id="upload" ref="upload" type="file" accept="image/*"            onChange={(event)=> { this.readFile(event) }}/> 

Below is what I tried:

<input id="upload" ref="upload" type="file" accept="image/*"                onChange={(event)=> {                     this.readFile(event)                     return false               }}/> 

Another one is:

<input id="upload" ref="upload" type="file" accept="image/*"            onChange={(event)=> {                 this.readFile(event)                 this.value=null           }}/> 

I believe above solutions work for jquery but I don't know how to let it work in reactjs.

like image 395
Joey Yi Zhao Avatar asked Sep 14 '16 07:09

Joey Yi Zhao


People also ask

How do you handle file input in React?

Select a File (user input): To enable the user to pick a file, the first step is to add the tag to our App component. This tag should have the type attribute set as “file”.


1 Answers

Dup of this thread

<input id="upload" ref="upload" type="file" accept="image/*"            onChange={(event)=> {                 this.readFile(event)            }}         onClick={(event)=> {                 event.target.value = null           }}  /> 
like image 187
James Bowler Avatar answered Sep 24 '22 05:09

James Bowler