2017-04-09 60 views
2

如何在上傳文件時停止打開文件上載窗口?e.preventDefault()或返回false無法停止文件輸入以打開

export default class Upload extends Component { 
    constructor(props) { 
     super(props); 

     this.state = { 
      filename: null, 
      isLoading: false 
     } 

    } 

    trigger_upload_input =() => { 
     document.getElementById("browse-file").click(); 
    } 

    handleOnChange = (e) => { 
     if(e.target.files[0]) { 
      this.setState({filename: e.target.files[0].name, isLoading: true}); 
      var that = this; 

      setTimeout(function(){ 
       that.setState({isLoading: false}) 
      },3000) 
     } 

    } 

    handleInputClicked = (e) => { 
     if(this.state.isLoading){ 
      e.preventDefault(); 
     } 
    } 

    render() { 
     const { isLoading } = this.state; 
     return(
      <div className="upload"> 
       <div> 
        <input onClick={this.handleInputClicked} onChange={this.handleOnChange} type="file" id="browse-file"/> 
        {this.state.filename && <span>{this.state.filename}</span>} 
       </div> 
      </div> 
     ) 
    } 
} 

負載指示工作,但是當我嘗試點擊再次輸入e.preventDefault()不停止文件窗口中打開,試圖return false它並沒有解決這個問題?

回答

0

嘗試e.stopPropagation(),可能會有幫助(event.stopPropagation())。

更新

外貌,像disabled屬性可以與onChange處理添加。這將防止對話框打開。加載完成後可以刪除此屬性。

+0

相同,不起作用。 –

+0

可能只是爲輸入添加一個「disabled」屬性以防止它被點擊? – ghostprgmr

相關問題