我把这个问题放在引用POST a file with React.js
我现在想将文件列表发送到端点。
我的输入组件:
<input onChange={(e) => Actions.uploadXLS(e.target.files)} multiple type="file" name="xlsz" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" style={{ display: 'none' }}/>
Action 处理程序:
uploadXLS(files) {
let i = 0;
const data = new FormData();
for ( i = 0; i < files.length; i++ ) {
data.append( 'file' , files[i]);
}
console.log(data);
this.getInstance().callUploadXLS( data );
}
控制台打印:FormData{}
最佳答案
File file = new File("/Users/user1/Documents/VendorDbResources/ExcelFileCore-1.xlsx");
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile1 = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
file = new File("/Users/user1/Documents/VendorDbResources/ExcelFileCore-2.xlsx");
input = new FileInputStream(file);
MultipartFile multipartFile2 = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
MultipartFile[] arrayOfMultipartFile = {multipartFile1, multipartFile2};
if (arrayOfMultipartFile.length > 0)
return vendorService.readExcelFile(arrayOfMultipartFile);
else
return null;
我已经尝试了使用静态数据的后端功能。在上面您可以看到我创建了一个多部分文件数组并提供给服务实现。
并且运行良好。条目也可以在数据库中看到。
关于javascript - 使用 React POST 多个多部分文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45545647/