【问题标题】:Passing and Accessing a JSON Object and a CSV file in a FormData在 FormData 中传递和访问 JSON 对象和 CSV 文件
【发布时间】:2018-08-20 19:02:31
【问题描述】:

我想在从前端到后端的 fetch 调用中传递 JSON Object 和 CSV file。

headerIngestion 是 JSON,我将文件保存在 csv 状态

let formData = new FormData();
formData.append('header', headerIngestion);
formData.append('file', this.state.csv);

fetch('http://localhost:8080/...', {
                method: 'POST',
                body: formData
            })
            .then ...

到目前为止,我的服务器使用 SpringBoot,因为我最初只传递了 csv 文件。

@PostMapping(URL)
public String ingestDataFile(@RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        byte[] bytes;
        try {
            bytes = file.getBytes();
            String completeData = new String(bytes);
            System.out.println(completeData);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return "something";
}

我需要做什么才能分别访问JSON 和file?

【问题讨论】:

    标签: javascript java json reactjs fetch


    【解决方案1】:

    玩了很久,终于成功了

    public String ingestDataFile(@RequestPart("header") String json,
            @RequestPart("file") MultipartFile file) {
    

    【讨论】:

      猜你喜欢
      • 2017-10-16
      • 2021-05-30
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 2019-10-01
      相关资源
      最近更新 更多