我的 reactjs 前端有一個類型為 file 的輸入字段。我想上傳一個 CSV 文件并將其發(fā)送到我使用 SpringBoot 的服務(wù)器。一旦該文件被提取到我的后端,我就遇到了如何獲取該文件的問題。這是我在渲染方法中的文件輸入:<input accept=".csv" type="file" onChange={this.handleChange}/><Button primary onClick={this.handleSubmit}> Submit </Button>處理提交:handleSubmit(event) { event.preventDefault(); let formData = new FormData(); formData.append('file', this.state.csv); fetch('http://localhost:8080/ingest', { method: 'POST', body: formData }) .then((res) => { var promiseStatus = res.text(); promiseStatus.then(function (value) { console.log(value); }); })}我的后端控制器是@PostMapping(INGEST_URL)public String ingestDataFile(@RequestBody ??? response) { return "temporary return";}應(yīng)該拿什么ingestDataFile()作為requestBody,比如說,打印文件的內(nèi)容。
2 回答

一只斗牛犬
TA貢獻1784條經(jīng)驗 獲得超2個贊
參數(shù)應(yīng)該是:
@PostMapping(INGEST_URL)
public String ingestDataFile(@RequestParam("file") MultipartFile file) {
System.out.println("Name is::: " + file.getName());
// Get file in byte[] using file.getBytes()
storeCSVInDbServer(file);
return "temporary return";
}
如果要將文件存儲在數(shù)據(jù)庫服務(wù)器中,可以將其存儲在數(shù)據(jù)庫的 BLOB 列中。
添加回答
舉報
0/150
提交
取消