[英]Receiving a MultiPart file and JSON data using Spring and AngularJS
Here's my controller below: 这是下面的我的控制器:
@RequestMapping(value="/upload", method=RequestMethod.PUT)
public ResponseEntity<String> handleFileUpload(
@RequestParam("file") MultipartFile file,
@RequestParam("data") String campaignJson,
Principal principal) throws JsonParseException, JsonMappingException, IOException {
I want to be able to receive a MultipartFile
as well as a JSON string containing data associated to the file (title, description, etc). 我希望能够接收
MultipartFile
以及一个包含与文件相关联的数据(标题,描述等)的JSON字符串。
I can get MultipartFile
uploading to work on it's own, and I can receive a JSON string and parse it on its own, but when I have them together in 1 controller, it fails. 我可以自己上传
MultipartFile
来工作,并且可以接收JSON字符串并自行解析,但是当我将它们放在1个控制器中时,它会失败。 Whenever I print out the String campaignJson
it says [object Object]
instead of the data that I'm sending (when I print out the data being sent in angular, it's in correct JSON format.) 每当我打印出
String campaignJson
它都会说[object Object]
而不是我要发送的数据(当我打印出以角度形式发送的数据时,它是正确的JSON格式。)
I've tried @RequestBody
, @RequestParam
, @RequestPart
, but to no avail. 我已经尝试过@
@RequestBody
,@ @RequestParam
,@ @RequestPart
,但无济于事。
My question is: How do I receive both a MultipartFile and data in the form of JSON in one Spring controller? 我的问题是:如何在一个Spring控制器中同时接收JSON形式的MultipartFile和数据?
This worked for me : 这为我工作:
@RequestMapping(value = "/{id}/upload", method = RequestMethod.PUT)
public DocumentResource uploadPut(@PathVariable("id") Long id,
MultipartHttpServletRequest request,
HttpServletResponse response) {
String next = request.getFileNames().next();
MultipartFile file = request.getFile(next);
.....
}
Edit : Using the MultipartHttpServletRequest
should not limit you in any way to use other @PathVariables
or @RequestParams
, so passing title and description should be possible. 编辑:使用
MultipartHttpServletRequest
不应以任何方式限制您使用其他@PathVariables
或@RequestParams
,因此传递标题和描述应该是可能的。
I used it to upload multiple images with AngularJS and FileUploader
我用它通过AngularJS和
FileUploader
上传了多个图像
( angular-file-upload v1.1.5 ) ( angular-file-upload v1.1.5 )
like this 像这样
var uploader = new FileUploader({
url: config.apiUrl('/machine/' + $scope.id + '/images/'),
method: "post",
queueLimit: maxFiles
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.