【发布时间】:2017-02-03 16:33:16
【问题描述】:
不幸的是this answer 对我没有帮助。问题似乎是由于某种原因,我的 POST 请求中不存在请求参数file。
我正在尝试在 POST 请求中上传文件,无论是二进制文件还是文本文件。 REST 控制器显示:
@PostMapping(WordEmbeddingApiPaths.UPLOAD_MODEL)
@RequestMapping(method=RequestMethod.POST, headers={"Content-Type=multipart/form-data"})
public ResponseEntity<WordVectorListDto> uploadModel(
@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
LOGGER.debug("POST uploadModel");
return new ResponseEntity<WordVectorListDto>((WordVectorListDto)null, HttpStatus.OK);
}
在我正在运行的客户端上:
var uploader = $scope.uploader = new FileUploader({
url: 'http://localhost:8080/rest-api/dl4j/we/uploadModel'
});
uploader.onAfterAddingFile = function($modelFile) {
console.info('onAfterAddingFile', $modelFile);
var fd = new FormData();
fd.append('file', $modelFile.file);
$http.post($modelFile.url, fd, {
headers: {
'Content-Type': 'multipart/form-data'
},
params: {'file' : $modelFile.file}
})
.then(
function (data) {
alert("upload success");
},
function (data, status) {
alert("upload error");
}
);
};
但是,我收到 400 Bad Request 作为服务器响应。
知道问题出在哪里吗?
更新:
我看到服务器端抛出了一个内部异常,说明:
org.springframework.web.multipart.support.MissingServletRequestPartException: 所需的请求部分“文件”不存在
我认为我已经设置了这个 - 我怎样才能做到这一点?
【问题讨论】:
-
请发布
FileUploader服务的模板和代码。
标签: angularjs spring angular-file-upload