【发布时间】:2016-02-25 08:23:08
【问题描述】:
到目前为止,我已经尝试了以下代码,其中我一直在 Angular js 中将文本和文件值附加到表单数据并将其发送到控制器,但是当我提交表单时,我得到 415 unsupported content type error in我的控制台 html
<html>
<head>
<script src="extensions/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="testcontrol">
<input type="text" ng-model="A.username" placeholder="Enter Username" required>
<input type="password" ng-model="A.password" placeholder="Enter Password" required>
<input type="file" placeholder="Browse image" name="file" id="test" required>
<input type="button" value="Send" ng-click="setValues()" />
<script>
var app = angular.module('myApp', []);
app.controller('testcontrol', function($scope, $http) {
$scope.setValues = function() {
var formData = new FormData();
var file = '#test';
var json = $scope.A;
formData.append("file", file);
formData.append("asd",JSON.stringify(json));
$http({
method : 'POST',
url : 'form/upload',
headers : {
'Content-Type' : 'undefined'
},
data : formData
}).success(function(data) {
alert(JSON.stringify(data));
});
};
});
</script>
</body>
</html>
控制器代码
@Controller
@RequestMapping(value="/form")
public class Form {
@RequestMapping(value = "/upload", method = RequestMethod.POST,consumes = {"multipart/form-data"})
public @ResponseBody
void storeAd(@RequestPart("asd") String adString, @RequestPart("file") MultipartFile file) throws IOException {
logger.info("entered controller");
TestDto1 jsonAd = new ObjectMapper().readValue(adString, TestDto1.class);
//do whatever you want with your file and jsonAd
}
【问题讨论】:
标签: angularjs json spring-mvc undefined