简体   繁体   中英

Spring boot and Angularjs Multipartfile and application/xml

It's my controller in spring boot

    @RequestMapping(value = "/test", method = RequestMethod.POST, consumes = "multipart/form-data")
    @ResponseBody
    public ResponseEntity<?> test(
            @RequestParam("enveloppe") Enveloppe enveloppe,
            @RequestParam("files") MultipartFile[] uploadFiles) {


    }

It's my service in angularJs

var fd = new FormData();
    angular.forEach(files, function(file) {
        fd.append('files', file);
    })
    fd.append('enveloppe', enveloppe, {type :'application/xml'} );
    $http.post("/test", fd, {
        transformRequest : angular.identity,
        headers : {
            'Content-Type' : undefined
        }

My object "enveloppe" generated automatically by xsd in spring boot. my object "enveloppe" in angularjs converted by json2Xml lib to have xml. But, spring boot cant' wrapper the object enveloppe. ~

1 ) it's possible to do that ?

Yes it is possible but instead appending in formData in service, i would say do it in controller

<input type="file" id="filename">

In your controller

var file=document.getElementById("filename").files[0];
var formData = new FormData();
formData.append("fileKey", file);

then send data to backend using your service.

I found the solution with spring boot and xml for the second part with @RequestPart

My controller

@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = "multipart/form-data")
    @ResponseBody
    public ResponseEntity<?> test(
            @RequestPart("enveloppe") Enveloppe enveloppe,
            @RequestPart("files") MultipartFile[] uploadFiles) {


    }

angularjs service

var fd = new FormData();
        angular.forEach(files, function(file) {
            fd.append('files', file);
        })
        fd.append('enveloppe', new Blob([enveloppe], {type: 'application/xml'}));

        $http.post("/test", fd, {
            transformRequest : angular.identity,
            headers : {
                'Content-Type' : undefined
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM