Empty List when trying to upload many files in Spring with ng-file-upload

前端 未结 5 1035
遇见更好的自我
遇见更好的自我 2020-12-14 02:39

I have the following controller method for uploading multiple files at once, inspired by this blog post and answers to this question as well:

@RequestMapping         


        
5条回答
  •  天命终不由人
    2020-12-14 02:55

    That works for me, sending big 'email' object with multiple file attachments from UI to back-end:

    Angular

    sendEmailWithAttachments(taskId: string, template: string, email: any, modelConfig: any, files: any[]) {
        let formData = new FormData();
        formData.append('form', new Blob([JSON.stringify(email)], {type: 'application/json'}));
        files.forEach(file  => {
            formData.append('files', file);
        });
    
        return this.$http({
            method: 'POST',
            data: formData,
            url: this.baseUrl + '/' + taskId + '/email-with-attachment?template=' + template,
            headers: {
                'Content-Type': undefined
            },
            responseType: 'arraybuffer'
        });
    }
    

    Java Spring

    @RequestMapping(value = "{taskId}/email-with-attachment", method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE)
    public void sendEmailWithAttachment(
            @PathVariable String taskId,
            @RequestParam String template,
            @RequestParam("form") MultipartFile form,
            @RequestParam("files") List files) throws IOException {
        Map parameters = new ObjectMapper().readValue(form.getInputStream(), HashMap.class);
    
        System.out.println("taskId", taskId);
        System.out.println("template", template);
        System.out.println("files", files);
        System.out.println("parameters", parameters);
    }
    

提交回复
热议问题