Image Preview Before upload image into ReactJs

 getInitialState: function(){

  return{file: []}

}

_onChange: function(){

  // Assuming only image

  var file = this.refs.file.files[0];

  var reader = new FileReader();

  var url = reader.readAsDataURL(file);


   reader.onloadend = function (e) {

      this.setState({

          imgSrc: [reader.result];

      })

    }.bind(this);

  console.log(url) // Would see a path?

  // TODO: concat files

},


render: function(){

 return(

  <div>

    <form>

      <input 

        ref="file" 

        type="file" 

        name="user[image]" 

        multiple="true"

        onChange={this_onChange}/>

     </form>

    {/* Only show first image, for now. */}

    <img src={this.state.imgSrc} />

  </div>

 )

}

Comments

Popular posts from this blog

Format mobile number to this format "(845) 963-2545" #mobileFormat