首页 > 解决方案 > 无法从模板中保存带有其他文本详细信息的文件,这会导致“所需的请求部分‘文件’不存在”这个错误

问题描述

我的控制器

@PostMapping
public String registerUserAccount(@ModelAttribute("user") @Valid User user,
BindingResult result, @RequestParam("file") MultipartFile file) throws IOException{

    byte[] pic = file.getBytes();    
    User imageUser = new User(pic);  
    userService.save(user);
    userService.save(imageUser);

    return "redirect:/registration?success";
    }

我正在通过服务保存来自控制器的图像和其他文本细节

我的模板

<form class="login100-form validate-form" th:action="@{/registration}" th:object="${user}" method="post" enctype="multipart/form-data">

    //other textual inputs like name address and number
    
<label for="Profile Picture">Profile Picture</label>
<input  type="file" name="file" placeholder="Pofile Picture" th:field="*{pic}">
</div>

标签: spring-bootthymeleaf

解决方案


推荐阅读