ng-file-upload多个文件到spring rest服务

时间:2016-03-24 09:40:59

标签: spring file ng-file-upload

我正在使用ng-file-upload将文件上传到Spring rest服务。单个文件可以成功上传,但在多个文件时失败 我使用的代码如下:
JavaScript的:

                    $scope.uploadFiles = function (files) {
                    $scope.files = files;
                    if (files && files.length) {
                        Upload.upload({
                            url: 'http://localhost:8099/test/upload2',
                            data: {files: files}
                        }).then(function (response) {
                            $timeout(function () {
                                $scope.result = response.data;
                            });
                        }, function (response) {
                            if (response.status > 0) {
                                $scope.errorMsg = response.status + ': ' + response.data;
                            }
                        }, function (evt) {
                            $scope.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
                        });
                    }
                };

Java:

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/upload2")
public void upload2(@RequestParam("files") MultipartFile[] files) throws IOException {
}

但是文件数组是空的,多个文件有什么问题?

UPDATE1:
我可以在网络标签中看到如下请求:

------WebKitFormBoundarywCLpwRaJRcmfPiBl
Content-Disposition: form-data; name="username"

qqq
------WebKitFormBoundarywCLpwRaJRcmfPiBl
Content-Disposition: form-data; name="files[0]"; filename="eagle.jpg"
Content-Type: image/jpeg


------WebKitFormBoundarywCLpwRaJRcmfPiBl
Content-Disposition: form-data; name="files[1]"; filename="eclipse.epf"
Content-Type: application/octet-stream


------WebKitFormBoundarywCLpwRaJRcmfPiBl--

1 个答案:

答案 0 :(得分:4)

通过在发布请求时添加参数来解决:arrayKey:''

请参阅:Empty List<MultipartFile> when trying to upload many files in Spring with ng-file-upload