2017-04-09 57 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

相关问题