Java 角度弹簧文件上传
我正试图点击Spring RestController并得到: o、 s.web.servlet.PageNotFound:不支持请求方法“POST” 我相信控制器的地图上少了一些东西Java 角度弹簧文件上传,java,angularjs,spring,spring-mvc,Java,Angularjs,Spring,Spring Mvc,我正试图点击Spring RestController并得到: o、 s.web.servlet.PageNotFound:不支持请求方法“POST” 我相信控制器的地图上少了一些东西 <div class="col-sm-1" style="background-color:#cccccc;" align="center"><span class="file-input btn btn-primary btn-file">Import file<input typ
<div class="col-sm-1" style="background-color:#cccccc;" align="center"><span class="file-input btn btn-primary btn-file">Import file<input type="file" onchange="angular.element(this).scope().uploadScriptFile(this.files)"></input></span></div>
$scope.uploadCtrFile = function(files) {
console.log(">>>>>>>>>>>>>>>>uploadCtrFile");
var fd = new FormData();
//Take the first selected file
fd.append("file", files[0]);
console.log(">>>>>>>>>>>>>>>>uploadCtrFile angular.toJson: "
+ angular.toJson(fd, 2));
$http.post('/rest/uploadCtrFile/', fd,{
withCredentials: true,
headers: {'Content-Type': undefined },
transformRequest: angular.identity
}).success(function(fd, status, headers, config) {
$scope.success = ">>>>>>>>>>>>>>>>uploadCtrFile Success: "+JSON.stringify({data: fd});
console.log($scope.success);
})
.error(function(fd, status, headers, config) {
$scope.success = ( "failure message: " + JSON.stringify({data: fd}));
console.log($scope.success);
});
};
我缺少什么?您将
未定义的作为内容类型的值发送,如下所示:
headers: {'Content-Type': undefined }
但是控制器需要内容类型
和多部分/表单数据
值:
@RequestMapping(..., headers = "'Content-Type': 'multipart/form-data'", ...)
您应该在请求中发送正确的内容类型
标题,如:
headers: {'Content-Type': 'multipart/form-data'}
或者从控制器定义中删除标题
选项:
@RequestMapping(value = "/uploadCtrFile/", method = RequestMethod.POST)
headers: {'Content-Type': 'multipart/form-data'}
@RequestMapping(value = "/uploadCtrFile/", method = RequestMethod.POST)