【问题标题】:Post a BLOB image using Ajax and receive using Spring Boot使用 Ajax 发布 BLOB 图像并使用 Spring Boot 接收
【发布时间】:2017-01-22 04:40:16
【问题描述】:

我正在尝试使用 ajax 发布 BLOB 图像。但是 spring 控制器正在接收一个空值。

这是我的控制器。

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

    // do some stuff
}

这里是 ajax 部分。

    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,
    });

【问题讨论】:

    标签: ajax spring spring-boot


    【解决方案1】:

    您需要 MultipartFile 来接收文件。

    @RequestMapping(value = "/uploadAvatar", method = RequestMethod.POST)
    public @ResponseBody List<Long> uploadAvatar(@RequestParam MultipartFile  avatar) {
        byte[] bytes = avatar.getBytes();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-04
      • 2021-12-19
      • 2015-09-13
      • 1970-01-01
      • 2022-12-29
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      相关资源
      最近更新 更多