【问题标题】:Sending Array of MultipartFile using react and spring boot and receiving null使用 react 和 spring boot 发送 MultipartFile 数组并接收 null
【发布时间】:2022-12-29 00:42:08
【问题描述】:

我正在尝试通过对 Spring 引导的反应发送一个多部分文件数组和一个 json,但我收到空指针异常。我想提一下,如果我使用邮递员发送请求,一切都很好。

我有以下使用 Spring 引导的控制器

    public ResponseEntity<Post> createPost(@RequestParam(value = "files",required = false) MultipartFile[] files, @RequestParam(value = "post", required = false) String post, @PathVariable UUID userId) throws Exception {
        ObjectMapper mapper = getJsonParserMapper();
        Post createdPost = mapper.readValue(post, Post.class);
        return ResponseEntity.status(HttpStatus.CREATED).body(postService.createPost(files, createdPost, userId));
    }

以及以下获取方法:

export async function createPost(accessToken, files, title, description, selectedUsers, owner, facultySet, lat, lng, price) {
  
  let data = new FormData();

  let post = '{"userSet": ' + selectedUsers + ', "description":"' + description + '", "title":"' + title + '", "price":' + price + ', "lat":' + lat + ',"lng":' + lng + ', "facultySet":' + facultySet + '}'
  data.append("post", post);

  // for (const file of files) {
  //  data.append('files', file)
  // }

  data.append('files', files)

  let updateUserUrl = CREATE_POST_ENDPOINT_URL(owner);
  let response;
  let resp = await fetch(updateUserUrl, {
    method: "POST",
    headers: {
      Authorization: "Bearer " + accessToken
    },
    body: data
  });


  if (resp.status !== 200) {
    response = {
      status: resp.status,
      statusText: getReasonPhrase(resp.status),
    };
  } else {
    let respData = await resp.json();

    response = {
      status: resp.status,
      data: respData,
    };
  }
}

我已经看到,如果我想发送一组文件,我应该一个一个地发送
for (const file of files) data.append('files', file) 但它似乎不起作用。 this is a screeenshot from postman

提前致谢!

【问题讨论】:

    标签: reactjs spring-boot fetch


    【解决方案1】:

    【讨论】:

      猜你喜欢
      • 2017-11-14
      • 1970-01-01
      • 2016-11-03
      • 2019-04-17
      • 2021-05-29
      • 2019-08-03
      • 2018-10-13
      • 2020-11-16
      • 2017-09-17
      相关资源
      最近更新 更多