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

222
Visualizações
Error 404 when uploading file with AJAX to Java Spring

i've been struggling for a while to send a file from html to my Java controller using AJAX and my nerves got to an end. I've read multiple answers on stack overflow but nothing worked for me. I am using Brave as browser.

This is my HTML:

<div>
   <button type="button" onclick="uploadFile()">Import</button>
     <div class="custom-buttons">
        <button onclick="clickRealFile()"type="button" name="button">Pick file</button>
         <input onchange="changeInputText(this)" id="real-file-second" type="file" name="file" hidden="hidden">
    <p style="margin-left: 0.3rem;" id="custom-text">Upload file</p>
     </div>
</div>

Here i just use a fake input to style the "pick file" button, don't mind what's happening here because in javascript the file gets picked correctly

JAVASCRIPT

function uploadFile() {
    let formData = new FormData();
    console.log($("#real-file-second")[0].files[0])
    formData.append("file", $("#real-file-second")[0].files[0]);
    $.ajax({
        url: encodeURI(window.location.origin + "/upload"),
        type: 'POST',
        contentType: false,
        cache: false,
        processData: false,
        data: formData

    })
}

Java Controller

@PostMapping("/upload")
public void importProducts(@RequestParam("file") MultipartFile file) throws IOException {
System.out.println("I've reached here");
}

Everything i've try ends up in 404 error. I don't know if there is a problem with my browser or with my code but all the solutions from internet are not working for me

about 4 years ago · Juan Pablo Isaza
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