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

121
Visualizações
getting empty object while sending formData

I'm trying to upload an image and then send it to the server. But turns out I'm sending an empty object to the server. I'm not using any form. Just through input tag. Is the code correct?

html:

 <div>
    <input type="file" class="fileUpload" name="file"/>
 </div>

js:

 document.querySelector('.fileUpload').addEventListener("change", function(e){

       let formData = new FormData();
       formData.append('imageFiles', e.target.files[0])



     // for(var v of formData) {
     //   console.log(v[0]+', '+v[1]);
     // } i tried to check here and it gives `imageFiles, [object File]`. This made me 
      // think there is data so sent it to server.
       

       fetch(`/image`, {
         method: 'POST',
         headers: { 'Content-Type': 'application/json' },
         body: JSON.stringify(formData)
        }).then(res => console.log(res))
        .then(json => console.log(json))
        .catch(err => console.log(err));
  }) 

node.js:

 app.post('/image', (req, resp) => {

  console.log(req.body)// but get empty object on server {}

})
about 4 years ago · Santiago Trujillo
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