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

220
Views
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
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!