2

I'm trying to post an BLOB image using ajax. But spring controller is receiving a null value instead.

Here is my controller.

@RequestMapping(value = "/uploadAvatar", method = RequestMethod.POST)
public @ResponseBody List<Long> uploadAvatar(byte[] avatar) {
    // avatar is null here

    // do some stuff
}

And here is the ajax part.

    var avatar = // some BLOB data
    var fd = new FormData();
    fd.append('fname', 'avatar.png');
    fd.append('avatar', avatar);

    $.ajax({
        url: '/uploadAvatar',
        type: 'POST',
        data: fd,
        cache: false,
        processData: false,
        contentType: false,
        success: applySuggestions,
    });

1 Answer 1

1

you need MultipartFile to receive file.

@RequestMapping(value = "/uploadAvatar", method = RequestMethod.POST)
public @ResponseBody List<Long> uploadAvatar(@RequestParam MultipartFile  avatar) {
    byte[] bytes = avatar.getBytes();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.