[英]Unable to upload file from Angular client to Spring Java server: Says 400 error
When I am uploading a file from Postman rest client to the server(spring application deployed on a remote machine), I am able to upload the file without any issue. 当我将文件从Postman rest客户端上传到服务器(部署在远程计算机上的spring应用程序)时,我可以上传文件而没有任何问题。
But when I try to write a rest client in angular.js, and send over the request, I get 400 Bad Request Error. 但是,当我尝试在angular.js中编写一个rest客户端并发送请求时,出现400错误请求错误。 I know it's because of some syntax issue between what is send from client and what server is expecting.
我知道这是由于客户端发送的内容与服务器期望的内容之间存在语法问题。
Server side code: 服务器端代码:
@CrossOrigin(origins = "*") @RequestMapping(value="/upload", method=RequestMethod.POST, consumes = {"multipart/form-data"}) @CrossOrigin(origins =“ *”)@RequestMapping(value =“ / upload”,method = RequestMethod.POST,消耗= {“ multipart / form-data”})
public @ResponseBody String handleFileUpload(@RequestParam("files") MultipartFile file){ . public @ResponseBody字符串handleFileUpload(@RequestParam(“ files”)MultipartFile file){。 .
。 .
。 .
。 }
}
Client side code: 客户端代码:
$scope.uploadFiles = function () {
alert("inside");
var request = {
method: 'POST',
url: 'http://IP ADDRESS and PORT NUMBER/upload',
data: formdata,
headers: {
'Content-Type': undefined
}
};
$http(request)
.success(function (response) {
alert("success: "+response);
})
.error(function (err) {alert("error: "+err);
});
}
I have uploaded the code here for file upload using Spring and AngularJS: 我已经在此处上传了使用Spring和AngularJS上传文件的代码:
https://gist.github.com/abdulrafique/9219f7164fdf5dc6dfa8da110be6a04e https://gist.github.com/abdulrafique/9219f7164fdf5dc6dfa8da110be6a04e
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.