Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
How can I upload multiple files in a React form to Flask API?

I am experimenting with React and I am trying to upload multiple images to my API in flask for saving. I was able to figure out how to upload a single file, but am struggling to convert to multiple. Here is the code for single upload.

FLASK

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

REACT

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 Respostas
Responde à pergunta

0

You could set the multiple property on your file input so you can select multiple files:

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

Then you could change your handleUploadImage function so it sends all files selected:

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda