1

I'm using angularJs and spring 4.0,

My Controller code:

@RequestMapping(value = "/endpoint/add", method = RequestMethod.POST)
public @ResponseBody GenericFormResponse execute(
    WebRequest wreq,
    @RequestParam("epName") String epName,
    @RequestParam("ipAddr") String ipAddr,
    @RequestParam("useDefault") String useDefault,
    @RequestParam("certFile") MultipartFile certFile) throws Exception {
    .....................
}

my js(angualrJs) code:

var formData = new FormData();
formData.append("epName", $scope.epName);
formData.append("ipAddr", $scope.ipAddr);
formData.append("useDefault",$scope.defaultCert);
if(!$scope.defaultCert){
    formData.append("certFile", upFile);
}
$http({
    method: "POST",
    url: "./service/endpoint/add",
    data: formData,
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined }
}).success(svcSuccessHandler)
  .error(svcErrorHandler);

My problem is $scope.defaultCert=false the POST request is working fine, $scope.defaultCert = true i'm getting Bad request(400).

i tried below thing also,

if(!$scope.defaultCert){
    formData.append("certFile", upFile);
}else{
    formData.append("certFile", null);
}

How do i sent empty MultipartFile. Thanks.

Rajesh Narravula
  • 1,433
  • 3
  • 26
  • 54

2 Answers2

0

I created two services in controller, and two urls for both

@RequestMapping(value = "/endpoint/add-with-cert", method = RequestMethod.POST)
public @ResponseBody GenericFormResponse excecuteWithCert(
        WebRequest wreq,
        @RequestParam("epName") String epName,
        @RequestParam("ipAddr") String ipAddr,
        @RequestParam("useDefault") boolean useDefault,
        @RequestParam("certFile") MultipartFile certFile) throws Exception {
    LOGGER.debug("received request for endpoint creation with certificate");
    GenericFormResponse response = new GenericFormResponse();
    SessionManager sessionMgr = new SessionManager(wreq);
    if(!sessionMgr.isLoggedIn()) {
        response.setSuccess(false);
        response.setGlobalErrorCode("not_logged_in");
        return response;
    }
    ...............
}

@RequestMapping(value = "/endpoint/add", method = RequestMethod.POST)
public @ResponseBody GenericFormResponse excecuteWithOutCert(
        WebRequest wreq,
        @RequestParam("epName") String epName,
        @RequestParam("ipAddr") String ipAddr,
        @RequestParam("useDefault") boolean useDefault) throws Exception {
    LOGGER.debug("received request for endpoint creation  without certificate");
    ...............
}

in js file:

var url = "./service/endpoint/add";
if(!$scope.defaultCert){
    formData.append("certFile", upFile);
    url = "./service/endpoint/add-with-cert";
}       
$http({
method: "POST",
    url: url,
    data: formData,
    transformRequest: angular.identity, headers: {'Content-Type': undefined }
}).success(svcSuccessHandler)
.error(svcErrorHandler);

I don't know is this correct approach or not, but it is fulfilled my requirement. working as i expect. Please suggest best answers.

Rajesh Narravula
  • 1,433
  • 3
  • 26
  • 54
-1

You need to configure properly to enable multipart file upload in Spring 4.

Add the following line to application initializer class:

dispatcher.setMultipartConfig(
        new MultipartConfigElement("/tmp", 25 * 1024 * 1024, 125 * 1024 * 1024, 1 * 1024 * 1024)
);

Here dispatcher is an instance of ServletRegistration.Dynamic

Refer this answer for more details.

Community
  • 1
  • 1
Yash
  • 182
  • 1
  • 4