Answers for "preview image before upload reactjs"

8

show preview of input image javascript react

const React = require('react')
class Upload extends React.Component {
  constructor(props){
    super(props)
    this.state = {
      file: null
    }
    this.handleChange = this.handleChange.bind(this)
  }
  handleChange(event) {
    this.setState({
      file: URL.createObjectURL(event.target.files[0])
    })
  }
  render() {
    return (
      <div>
        <input type="file" onChange={this.handleChange}/>
        <img src={this.state.file}/>
      </div>
    );
  }
}
module.exports = Upload
Posted by: Guest on May-27-2020
1

display preview of image upload react

//ReactJs

import {useState } from "react";

export default function Home() {
	 const [file,  setFile] = useState("")
     
     const handleChange = (e) =>{
       setFile(URL.createObjectURL(e.target.files[0]))
     }
     
     return (
         <div>
          <input type="file" onChange={handleChange}/>
          <img src={file}/>
        </div>
     )
	
}
Posted by: Guest on August-06-2021
1

display preview of image upload react

<!-- html + Javascript -->

//js
<script>
  const handleChange = (e) =>{
     const file = URL.createObjectURL(e.target.files[0])
     document.getElementById("preview-img").src = file;
  }
  document.getElementById("file-input").addEventListener("change", handleChange);
</script>
     

 //html
 <div>
  <input id="file-input" type="file"/>
  <img id="preview-img" src="" width="200"/>
</div>
Posted by: Guest on August-06-2021
0

preview image before upload reactjs

state = {
  img: logo
}

handleChangeImage = e => {
  this.setState({[e.target.name]: URL.createObjectURL(e.target.files[0])})
}

<input type="file" id="img" name="img" accept="image/*" className="w-100" onChange={this.handleChangeImage}/>

<img src={this.state.img} alt="img"/>
Posted by: Guest on September-02-2021

Code answers related to "preview image before upload reactjs"

Code answers related to "Javascript"

Browse Popular Code Answers by Language