前端js 提交表单数据-后端处理方法(多附件提交)

本文讲述了前端在提交表单时遇到`Failedtoconvertjava.lang.Stringtojava.util.List`的问题,介绍如何正确处理多附件上传,避免对象转换错误,通过将`examPaper`列表逐个append到FormData中来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我们前端进行表单提交的时候,有时候会出现这种情况:Failed to convert   java.lang.String    to java.util.List

等等。

例如:

  我后台定义一个对象:

       examPaper 包含  String userId,Float userScore, MultipartFile  examFile  用户id  ,试卷分数,试卷文件

对象外面   classPaper有: String classId  String className  List<examPaper>  examPaperList

这个时候,后台接收为  ClassPaper

如果按照平常的 form-data   提交  则应按以下方式提交:

let  fd  = new FormData();

fd.append("classId ",classId );

fd.append("className ",className );

examPaperList.forEach((item,index) ->{

     fd.append("examPaperList["+index+"].userId",item.userId);

     fd.append("examPaperList["+index+"].userScore",item.userScore);

     fd.append("examPaperList["+index+"].examFile ",item.examFile );

});

以这种方式就可以实现 多附件  一一 对应提交。以避免对象转换错误问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值