我正在努力在Spring控制器中上传多个部分的文件。我读了很多问题,谷歌,但似乎什么都不起作用。
我得到了
error: "Bad Request"
exception: "org.springframework.web.multipart.support.MissingServletRequestPartException"
message: "Required request part 'file' is not present"
我的BE控制器:
@RequestMapping(value = "/zip", method = RequestMethod.POST)
public void readFile(@RequestParam("file") MultipartFile file) throws IOException {
// code
}
FE,angularJS:
service.getZip = function getZip(file) {
var formData = new FormData();
formData.append('file', file);
return $http({
method: 'POST',
url: CONSTANTS.readFile,
data: formData,
headers: {'Content-Type': undefined}
}) .then(function (response) {
var data = response.data;
return data.id;
});
}
HTML:
<input type="file" id="file" name="file" accept=".txt"/>
此外,application.properties还包含:
spring.http.multipart.enabled=false
更新:
当遵循@Byeon0gam建议从我的控制器中删除@RequestParam时,我不再收到这个错误,但当我的文件到达控制器时,它为空。虽然在FE服务中,如我所见,它不是空的:
发布于 2018-10-04 02:27:29
将FE中的Content-Type更改为:
headers: {'Content-Type': 'x-www-form-urlencoded'}
希望它能为你工作。
https://stackoverflow.com/questions/52633452
复制相似问题