ng-file-upload:将列表作为附加数据发送

时间:2017-10-17 09:35:11

标签: angularjs spring-mvc ng-file-upload

我想使用ng-file-upload上传文件,并同时发送值列表(类型为Long)。
我做到了:
客户端

vm.uploadFiles = function(file, errFiles) {
    $scope.f = file;
    $scope.errFile = errFiles && errFiles[0];
    if (file) {
        file.upload = Upload.upload({
            url: '/api/sendMailCra',
            fields: {'collaborateursId':vm.collaborateursId},
            file: file
        });

        file.upload.then(function (response) {
            $timeout(function () {
                file.result = response.data; 
            });
        }, function (response) {
            vm.clear();
        }, function (evt) {
        });
    }
}

服务器端

@RequestMapping(value = "/sendMailCra",
        method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public void upload(@RequestParam("collaborateursId") List<Long> collaborateursId, @RequestParam("file") MultipartFile file) throws IOException {
    log.debug("REST send mail to Collaborateurs : {}"+collaborateursId);
}

我正在

  

500内部服务器错误

服务器端没有错误日志。

如何将我的列表从客户端传递到服务器端?
感谢

1 个答案:

答案 0 :(得分:0)

  

使用 form data ,我们可以将文件和数据从角度控制器发送到服务器。

vm.uploadFiles = function(file, errFiles) {
    $scope.f = file;
    $scope.errFile = errFiles && errFiles[0];
    var data = new FormData();
    data.append("uploadFile ", file);
    data.append("collaborateursId ", vm.collaborateursId);
    if (file) {
        file.upload = Upload.upload({
            url: '/api/sendMailCra',
            data:data,
            headers: {'Content-Type': undefined}
        });

        file.upload.then(function (response) {
            $timeout(function () {
                file.result = response.data; 
            });
        }, function (response) {
            vm.clear();
        }, function (evt) {
        });
    }
}

使用mutipartRequest

控制器
@RequestMapping(value = "/sendMailCra",
        method = RequestMethod.POST,
       )
@Timed
public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String collaborateursId= request.getParameter("collaborateursId");

    next converted the request to multipartRequest for get th file 
    MultipartHttpServletRequest mRequest  = (MultipartHttpServletRequest) request;
    Iterator<String> fileNames = mRequest.getFileNames();
            while (fileNames.hasNext()) {
//              HashMap<String, String> hashMap = new HashMap<String, String> ();
                MultipartFile file = mRequest.getFile(fileNames.next());

                store file into your prefered location....
                }

        log.debug("REST send mail to Collaborateurs : {}"+collaborateursId);
}

我们可以在spring configurationFile link

中设置文件大小限制
<beans:bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

         <!-- setting maximum upload size -->
        <beans:property name="maxUploadSize" value="100000" />

    </beans:bean>

请参阅此link