How to upload a file with Ajax in Spring Boot

In this brief guide, we shall learn how to upload a file in a Spring Boot web application using Ajax request. We will use jQuery on the client side for sending an asynchronously Ajax request.


Let's create a simple HTML form that enables users to select a file for upload:

<form id="file-upload-form">
    <label for="file-upload-input">Select file to upload</label>
    <input type="file" id="file-upload-input" name="file">
    <button type="submit">Start Upload</button>

jQuery Ajax Request

For an Ajax request, we will bind the onsubmit event for the form element that will be triggered when the form is submitted. Here is a jQuery code that listens for form submission event and sends the file to the server using the $.ajax() method:

$(document).ready(function () {
    // bind form submission event
    $("#file-upload-form").on("submit", function (e) {

        // cancel the default behavior

        // use $.ajax() to upload file
            url: "/file-upload",
            type: "POST",
            data: new FormData(this),
            enctype: 'multipart/form-data',
            processData: false,
            contentType: false,
            cache: false,
            success: function (res) {
            error: function (err) {

Spring Boot Controller

Finally, create a new method in the Spring Boot controller that listens for an HTTP POST request at the /file-upload end-point. This method handles the file upload request and saves the file in a local directory:

public ResponseEntity<String> fileUpload(MultipartFile file) {
    try {

        // upload directory - change it to your own
        String UPLOAD_DIR = "/opt/uploads";

        // create a path from the file name
        Path path = Paths.get(UPLOAD_DIR, file.getOriginalFilename());

        // save the file to `UPLOAD_DIR`
        // make sure you have permission to write
        Files.write(path, file.getBytes());
    catch (Exception ex) {
        return new ResponseEntity<>("Invalid file format!!", HttpStatus.BAD_REQUEST);

    return new ResponseEntity<>("File uploaded!!", HttpStatus.OK);

Maximum File Size

By default, Spring Boot only allows a maximum file size of 1MB. If you try to upload a larger file, you will encounter a MaxUploadSizeExceededException exception. But we can change this behavior by overriding multi-part properties in the file:

# max file size - default 1MB
# max request size - default 10MB

Now we can upload a file that can be larger than 1MB without worrying about any error on the server. It also increases the maximum request size to 50MB.

Further Reading

Want to learn more about handling files in Spring Boot? Check out our in-depth guides:

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.