简体   繁体   English

使用Spring和AngularJS接收MultiPart文件和JSON数据

[英]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.

相关问题 使用 multipart/form-data 或 multipart/mixed 获取 JSON 和文件时让 Spring 进行映射 - Getting Spring to map when getting JSON along with a file using multipart/form-data or multipart/mixed Spring 测试:发送数据文件的多部分 - Spring Tests: sending multipart for data file 嵌套的异常是java.lang.NumberFormatException:用于输入字符串/用于弹簧启动的Multipart文件和json数据 - nested exception is java.lang.NumberFormatException: For input string/ Multipart file and json data to spring boot Angularjs 将文件多部分 FormData 中的 null 值传递给 Spring MVC - Angularjs pass null value in file multipart FormData to Spring MVC 尝试使用 swagger 使用文件(多部分)生成带有 json(应用程序/json)数据的存根(java 文件) - Trying to generate stub(java file) with json(application/json) data with file(multipart) using swagger 使用Spring RestTemplate发布多部分(PDF)文件 - Post multipart (PDF) file using Spring RestTemplate 使用AngularJS的Java Spring Boot多部分POST请求 - Java Spring Boot multipart POST request using AngularJS 使用Spring数据在Elasticsearch中保存一个Json文件 - Save a Json file in elasticsearch using spring data React Spring 使用多部分表单数据启动应用程序 - 所需的请求部分“文件”不存在 - React Spring Boot App Using Multipart Form Data - Required request part 'file' is not present 如何在 Spring 引导中发送具有 json 值的多部分表单数据 - How to send multipart form data with json value in Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM