Angularjs 文件上传:js和SpringRESTAPI
我的任务是上传最大10MB的文档。我正在使用angular.js和SpringRESTAPI。然而,我有数据要上传文件张贴。 我的代码如下Angularjs 文件上传:js和SpringRESTAPI,angularjs,spring-mvc,Angularjs,Spring Mvc,我的任务是上传最大10MB的文档。我正在使用angular.js和SpringRESTAPI。然而,我有数据要上传文件张贴。 我的代码如下 $scope.submit=function(mymodel){ var request = { method: 'POST', url: '/myurl/update', headers: { 'Content-Type': undefined
$scope.submit=function(mymodel){
var request = {
method: 'POST',
url: '/myurl/update',
headers: {
'Content-Type': undefined
},
transformRequest: function(data) {
var formData = new FormData();
formData.append('mymodel', new Blob([angular.toJson(data.mydata)], {
type: "application/json"
}));
//angular.forEach( $scope.files,function(file){
formData.append('file', data.file);
//});
return formData;
},
data: {
data: mydata,
file: $scope.files[0]
}
}
$http(request).success(function() {
}).error(function() {
});
}
3. API code
@RequestMapping(value = "/update", method = RequestMethod.POST,consumes = {"multipart/form-data"}) //consumes = {"multipart/form-data"}
public @ResponseBody
void updateExceptionSummary(@RequestPart(value="mymodel") MyModel mymodel ,@RequestPart("file") MultipartFile file)
{
}
我得到以下错误:
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'mymodel' is not present.
at org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:194)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
有人能帮忙吗
我发现请求中发布的json
数据总是空的
有效载荷
------WebKitFormBoundaryJIRCCmCILVsxKIBc
Content-Disposition: form-data; name="mymodel"; filename="blob"
Content-Type: application/json
Content-Disposition: form-data; name="file"; filename="1.png"
Content-Type: image/png
------WebKitFormBoundaryJIRCCmCILVsxKIBc
Content-Disposition: form-data; name="mymodel"; filename="blob"
Content-Type: application/json
Content-Disposition: form-data; name="file"; filename="1.png"
Content-Type: image/png
使用
@modeldattribute Mymodel Mymodel
并将文件传递到模型本身。请参阅这两个文件和。现在将JSP代码转换为angular.Thanjay,但由于限制较少,我们无法在这里使用Model属性。我更新了下面的代码,上面的错误消失了。现在我面临另一个错误。@RequestMapping(value=“/update”,method=RequestMethod.POST,consumes={“multipart/form data”}//consumes={“multipart/form data”}public@ResponseBody void updateExceptionSummary(@RequestPart(value=“data”)Mymodel Mymodel,@RequestPart(“file”)MultipartFile file){var request={method:'POST',url:'/ews/exceptionsummary/update',headers:{'Content-Type':undefined},transformRequest:function(data){var-formData=new-formData();formData.append('data',new Blob([angular.toJson(data.data)],{type:'application/json'});formData.append('file',data.file);return formData;},data:{data:$scope.data,file:$scope.files[0]}org.springframework.web.multipart.support.MissingServletRequestPartException:所需的请求部分“数据”不存在。位于org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:194)位于org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78)