2017-07-05 39 views
0

我要上传的文件文件上传失败,angularjs春季启动

controller.js

userService.saveProfile($scope.user.profile.companyLogo).then(function(response){ 
    if(response.status==201){ 
     $('#profileModal').hide(); 
     $scope.load(); 
    } 
}); 

service.js

saveProfile:function(logo){ 
      var formData = new FormData(); 
      formData.append("file",logo); 
      var retVal = $http({ 
       method : 'POST', 
       url : '/api/user/profile', 
       transformRequest: angular.identity, 
       headers: {'Content-Type': undefined}, 
       data: formData 
      }).then(function(response){ 
       return response; 
      }); 
      return retVal; 
     } 

Controller.java

@RequestMapping(value="/profile",method = RequestMethod.POST,headers="'Content-Type':'multipart/form-data'") 
    public ResponseEntity saveProfile(@RequestBody MultipartFile file){ 
     System.out.println(file); 
     return new ResponseEntity(HttpStatus.CREATED); 
    } 

如果我将标题添加到@RequestMapping我得到404,如果我删除我得到的文件空

请建议我做的正确的方式上传文件

+0

您是否获得了服务内部文件的价值? – Vivz

+0

@Vivz是的,我在服务内获得价值 – poojadave

+0

它是在里面。然后呢? – Vivz

回答

0

它的工作!

我删除从@RequestMapping头和用于@RequestPart代替@RequestBody

@RequestMapping(value="/profile",method = RequestMethod.POST) 
    public ResponseEntity saveProfile(@RequestPart("file") MultipartFile file){ 
     System.out.println(file); 
     return new ResponseEntity(HttpStatus.CREATED); 
    }