Post Form data using AngularJS and Java Spring -


i have jsp file in uploading file. backend handling of file made controller in spring. return error in console: string parameter 'name' not present ? code -

jsp file

<input class="fileinput" type="file" id="fileinput" accept="image/gif, image/jpeg, image/png" /> <button type="button" ng-click="saveprofile()">update</button> 

js file

$scope.username = 01238339595; $scope.saveprofile = function() {                     var input = document.getelementbyid('fileinput');                     if (input.files && input.files[0]) {                         var formdata = new formdata();                         formdata.append("name", $scope.username);                         formdata.append("file", input.files[0]);                         console.log("form data " + formdata);                         $http.post("save-avatar", formdata)                             .success(function(data, status, headers,config) {                                                         console.log('success');                                                     })                             .error(function(data, status, headers, config) {                                                         console.log('error');                                                     });                     }                 }; 

controller

@requestmapping(value = "save-avatar", method = requestmethod.post) public string handleformupload(@requestparam("name") string name, @requestparam("file") multipartfile file,         httpservletrequest request) throws ioexception {     if (file.isempty())         throw new ioexception("file field empty");      servletcontext servletcontext = request.getsession().getservletcontext();     string absolutediskpath = servletcontext.getrealpath("/");      file folder = new file(absolutediskpath + "/avatar/");     if (!folder.exists())         folder.mkdirs();      file avatarfile = new file(folder, name + ".jpg");     if (!avatarfile.exists())         avatarfile.createnewfile();      fileoutputstream outputstream = null;     try {         outputstream = new fileoutputstream(avatarfile);         outputstream.write(file.getbytes());     } {         if (outputstream != null)             outputstream.close();     }      return "redirect:/avatar-profile?name=" + name; } 

in config xml:

<bean id="multipartresolver"     class="org.springframework.web.multipart.commons.commonsmultipartresolver">     <property name="maxuploadsize" value="10485760" /> </bean> 

instead of above controller method, use this:

@requestmapping(value = "/save-avatar", method = requestmethod.post) public void uploadfile(multiparthttpservletrequest request, httpservletresponse response) {      iterator<string> itr = request.getfilenames();     multipartfile file=null;      while (itr.hastnext()) {         file = request.getfile(itr.next());         string name = request.getparameter("name");          //do stuff here.......     } } 

Comments