[英]“the current request is not a multipart request” angularjs and spring boot
I know this question has been asked over and over, however after trying all the possible options still I get this error: 我知道这个问题已经被一遍又一遍地询问,但是在尝试了所有可能的选项后,我仍然收到此错误:
the current request is not a multipart
Here is my code in angular: 这是我的角度代码:
app.controller("controller", function ($scope, $http, $window, $log) {
$scope.upload = function () {
var file = $scope.myFile;
var fd = new FormData();
fd.append('file', file);
$http.post('upload', fd, {
transformRequest: angular.identity,
})
.success(function(){
})
.error(function(){
});
};
Controller code: 控制器代码:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public Callable<FileUpload> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
parser = fileHandlerService.getParser(file);
Callable<FileUpload> asyncResult = new Callable<FileUpload>() {
@Override
public FileUpload call() throws Exception {
FileUpload fileUpload = fileHandlerService.getUploadStatus();
return fileUpload;
}
};
return asyncResult;
}
HTML code: HTML代码:
<div class="vertical-center">
<input type="file" file-model="myFile">
<button ng-click="upload()" class="btn btn-primary btn-lg">Upload
</button>
</div>
I tried putting: headers: {'Content-Type': undefined }
however with adding header, I don't get error 500 anymore but I get error 400. 我尝试放置:
headers: {'Content-Type': undefined }
但是,添加标头后,我不再收到错误500,但收到错误400。
I send the file through Postman
without any headers, and it works fine. 我通过没有任何标题的
Postman
发送文件,并且工作正常。 So I don't understand why it doesn't work through angular. 所以我不明白为什么它不能通过角度工作。
Thanks in advance. 提前致谢。
In addition to adding headers: {'Content-Type': undefined } , you can change the div to a form and add enctype="multipart/form-data" to the form. 除了添加标头:{'Content-Type':undefined}之外,您还可以将div更改为表单,并将enctype =“ multipart / form-data”添加到表单。 ie your code would look like;
即您的代码看起来像;
<form class="vertical-center" enctype="multipart/form-data">
<input type="file" file-model="myFile">
<button ng-click="upload()" class="btn btn-primary btn-lg">Upload
</button>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.