当前位置:   article > 正文

VUE的formdata传集合List到Springboot后台_spring formdata传 list参数

spring formdata传 list参数

 vue中进行formdata的拼接

  1. // data中定义的list
  2. brandList: [{
  3. id: null,
  4. logoFile: null, // 文件上传
  5. backFile: null // 文件上传
  6. }],
  7. // 方法中进行formdata的拼接
  8. save() {
  9. const formdata = new FormData()
  10. this.brandList.forEach((item, index) => {
  11. formdata.append('list[' + index + '].id', item.id)
  12. formdata.append('list[' + index + '].logoFile', item.logoFile.raw, item.logoFile.name)
  13. formdata.append('list[' + index + '].backFile', item.backFile.raw, item.backFile.name)
  14. })
  15. api.save(formdata)
  16. .then(response => {
  17. this.$message.success('保存成功!')
  18. })
  19. }

springboot后台接收

  1. @Data
  2. public class BrandList implements Serializable{
  3. private static final long serialVersionUID = -8300392386535388817L;
  4. private List<BrandVO> list;
  5. }
  6. @Data
  7. public class BrandVO implements Serializable{
  8. private Long id;
  9. private MultipartFile logoFile;
  10. private MultipartFile backFile;
  11. }
  12. @ApiOperation(value = "批量新增数据")
  13. @PostMapping(value = "/v1/brands")
  14. public Response add(BrandList voList){
  15. List<brand> list = new ArrayList<>();
  16. Brand brand = null;
  17. for(BrandVO vo:voList.getList()){
  18. brand = new Brand();
  19. brand.setId(vo.getId());
  20. // 文件上传
  21. MultipartFile file = null;
  22. file = vo.getLogoFile();
  23. brand.setLogoUrl(fileUtil.getUrl(file));
  24. file = vo.getBackFile();
  25. brand.setBackUrl(fileUtil.getUrl(file));
  26. list.add(brand);
  27. }
  28. this.api.saveBatch(list);
  29. return ResponseUtil.returnSuccess();
  30. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号