MultipartFile数组不发送到弹簧控制器

时间:2016-06-30 17:57:49

标签: javascript java spring spring-mvc

我使用JS向我的spring控制器发送了一个post请求,但是我无法接收到的multipartFile数组是null。我发送3个单独的参数,如下所示。 (艺术作品,曲目和元数据)

    formData.append("artwork", $scope.image[0]);
    formData.append("tracks", $scope.files);
    formData.append("metadata",JSON.stringify(contentData));


    // $http.post('/review/' + $scope.objectID, formData).then("Successfully got objects !", "Successfully got objects !");
    $http({
    url: '/review/'+ $scope.objectID,
    method: 'POST',
    headers: {'Accept': 'application/json', 'Content-Type': undefined},
    data: formData,
    transformRequest: function (data, headersGetterFunction) {
      return data;
    }
  }).

我确实从参数接收元数据(这是一个字符串)和艺术作品(它是单个MultiPartFile),但是对于音轨(Array MultiPartFile)是null。

这是我的控制器

    @RequestMapping(value = "/review/{id}", method = RequestMethod.POST, produces = "application/json")
@ResponseStatus(value=HttpStatus.OK)
public void formHandler(
    @PathVariable("id") String id,
    @RequestPart("artwork") MultipartFile artwork,
    @RequestPart("tracks") MultipartFile[] tracks, 
    @RequestPart("metadata") String object) {


   // SubmissionsRequestObject subObject = getSubObject(object, artwork, tracks);
   // System.out.println(subObject.getAlbumname());
    System.out.println(object);
    System.out.println("-----");
   System.out.println(tracks.length);
   System.out.println(tracks);
   System.out.println("-----");
   for (MultipartFile t : tracks){
        System.out.println("count");
   }
   System.out.println(artwork.getOriginalFilename());
   // new SRP(subObject);



}

1 个答案:

答案 0 :(得分:2)

$scope.files追加到formData;)

时,您可以尝试这样做
for (var file in $scope.files) {
    formData.append("tracks", file);
}