猿问

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

我想在从前端到后端的 fetch 调用中传递 aJSON Object和 a 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?


元芳怎么了
浏览 337回答 1
1回答

温温酱

经过大量玩耍后,这奏效了public String ingestDataFile(@RequestPart("header") String json,         @RequestPart("file") MultipartFile file) {
随时随地看视频慕课网APP

相关分类

Java
我要回答