我有一个简单的html表单,我想用它来发送文本和文件到我的服务器。我已按照类似问题的答案进行了操作,并添加了enctype =" multipart / form-data"为我的形式。
我的表格是:
<form enctype="multipart/form-data"name="create" action="/person" method="post" >
<ul>
<li>
<label for="name">Name</label>
<input type="text" name="name" placeholder="" required>
</li>
</ul>
<ul>
<li>
<label for="picture">Picture</label>
<input type="file" name="picture" />
</li>
</ul>
<ul>
<li>
<input type="submit" value="Create">
</li>
</ul>
</form>
和我的Controller(Spring Boot)需要以下内容:
@RequestMapping(value="/person", method = RequestMethod.POST)
public void create(@RequestParam String name,
@RequestParam MultipartFile picture)
{...}
我得到以下结果(提交填写了所有必填字段并选择上传文件的完整表单时):
There was an unexpected error (type=Bad Request, status=400).
Required String parameter 'name' is not present
从表单中删除与文件相关的内容,Controller会给出预期的结果,并按预期发送文本信息。
有什么想法吗?我错过了这段代码中明显的东西吗?
答案 0 :(得分:0)
试试这个:
<form enctype="multipart/form-data" name="create" action="/person" method="post" >
<ul>
<li>
<label for="name">Name</label>
<input type="text" name="name" placeholder="" required="required" id="name"/>
</li>
</ul>
<ul>
<li>
<label for="picture">Picture</label>
<input type="file" name="picture" id="picture" />
</li>
</ul>
<ul>
<li>
<input type="submit" value="Create"/>
</li>
</ul>
</form>
和控制器:
@RequestMapping(value="/person", method = RequestMethod.POST)
public void create(@RequestParam(name = "name") String name,
@RequestParam(name = "picture") MultipartFile picture)