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

1.1K
Views
¿Cómo puedo cargar varios archivos en un formulario React a Flask API?

Estoy experimentando con React y estoy tratando de cargar varias imágenes en mi API en el matraz para guardarlas. Pude descubrir cómo cargar un solo archivo, pero estoy luchando para convertirlo en varios. Aquí está el código para una sola carga.

MATRAZ

 @app.route('/upload', methods={"POST"}) def upload_file(): file = request.files.getlist("file") print(file) response="Whatever you wish to return" return response

REACCIONAR

 export default class Test extends React.Component { constructor(props) { super(props); this.state = { }; this.handleUploadImage = this.handleUploadImage.bind(this); } handleUploadImage(ev) { ev.preventDefault(); const data = new FormData(); data.append('file', this.uploadInput.files[0]); fetch('http://localhost:5000/upload', { method: 'POST', body: data, }).then((response) => { response.json().then((body) => { }); }); } render() { return ( <form onSubmit={this.handleUploadImage}> <div> <input ref={(ref) => { this.uploadInput = ref; }} type="file" /> </div> <br /> <div> <button>Upload</button> </div> </form> ); } }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede establecer la propiedad multiple en la input de su archivo para que pueda seleccionar varios archivos:

 <input ref={(ref) => { this.uploadInput = ref; }} type="file" multiple />

Luego, podría cambiar su función handleUploadImage para que envíe todos los archivos seleccionados:

 handleUploadImage(ev) { ev.preventDefault(); const data = new FormData(); for (let i = 0; i < this.uploadInput.files.length; i++) { data.append("file", this.uploadInput.files[i]); } fetch("http://localhost:5000/upload", { method: "POST", body: data, }).then((response) => { response.json().then((res) => { console.log(res); }); }); }
over 4 years ago · Santiago Trujillo 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!