【问题标题】:Spring boot and Angularjs Multipartfile and application/xmlSpring boot 和 Angularjs Multipartfile 和 application/xml
【发布时间】:2017-09-17 01:08:58
【问题描述】:

这是我在 Spring Boot 中的控制器

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


    }

这是我在 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
        }

我的对象“信封”由 xsd 在 spring boot 中自动生成。 我在 angularjs 中的对象“enveloppe”由 json2Xml lib 转换为具有 xml。 但是,弹簧靴不能'包装对象信封。 ~

1) 可以这样做吗?

【问题讨论】:

标签: java angularjs xml spring-boot multipartform-data


【解决方案1】:

是的,这是可能的,但不是在服务中附加 formData,我会说在控制器中进行

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

在你的控制器中

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

然后使用您的服务将数据发送到后端。

【讨论】:

  • 我有技术限制,我有文件列表,我可以一起发送6个文件。
【解决方案2】:

我用 @RequestPart 找到了第二部分的 spring boot 和 xml 解决方案

我的控制器

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


    }

angularjs 服务

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
            }

【讨论】:

    猜你喜欢
    • 2017-03-08
    • 2017-01-12
    • 2017-11-27
    • 1970-01-01
    • 2017-11-14
    • 2019-08-13
    • 2016-12-24
    • 1970-01-01
    • 2019-06-16
    相关资源
    最近更新 更多