简体   繁体   English

在FormData中传递和访问JSON对象和CSV文件

[英]Passing and Accessing a JSON Object and a CSV file in a FormData

I want to pass a JSON Object and a CSV file in a fetch call from my frontend to my backend. 我想在从前端到后端的访存调用中传递JSON ObjectCSV file

headerIngestion is the JSON and I have the file saved in the csv state 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 ...

I have this so far for my sever that uses SpringBoot as originally i only passed the csv file. 到目前为止,我已经为使用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";
}

What do I need to do to be able to access the JSON and the file separately? 为了能够分别访问JSONfile我需要做什么?

After a lot of playing around, this worked 经过大量的玩耍,这工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM