Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
Cómo obtener datos de imagen de una entrada de archivo en react.js

Quiero obtener los datos de un archivo de entrada en reaccionar. He creado una clase con los datos como estado. La idea es que los datos se actualicen cada vez que cambie la entrada. Mientras esto sucede, una imagen debería cargarse con los datos de entrada.

El siguiente código es lo que he creado, pero no funciona.

 class ImagePicker extends React.Component { constructor(props){ super(props); this.state = { data: null } } handleDataChange = e => { this.setState({ data: e.target.value }) } render(){ return( <div> <input type='file' onChange={this.handleDataChange} /> <img src={this.state.data}></img> </div> ) } }

No sé cuál es el problema, ¿podrías ayudarme?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puedes usar URL.createObjectURL

 handleDataChange = event => { if (event.target.files && event.target.files[0]) { this.setState({ data: URL.createObjectURL(event.target.files[0]) }); } }
about 4 years ago · Juan Pablo Isaza Report

0

Esto resuelve el problema:

 class ImagePicker 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> ); } }

Al elegir un archivo, elige una URL para ese archivo. Puede convertir esta URL en un archivo png usando URL.createObjectURL().

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!