0

I am new to angularjs and i am not able to understand the examples on internet for angularjs file upload directive. I have a following class :

@Entity
public class StudentPicture implements Serializable {
private static final long serialVersionUID = 1L;
@Id private String studentId;
private byte[] picture;
// getters setters
}

And i want to save the picture in database So far i have done this Controller

@RequestMapping(value = "/sampletest", method = RequestMethod.POST)
public void mySample(@RequestBody StudentPicture studentPicture) {

    System.out.println(studentPicture.getStudentId());
}

My jsp form is

var sample = angular.module('sample', [ "xeditable", "ui.bootstrap" ])
.config(function($httpProvider) {       
    $httpProvider.defaults.headers.common["X-Requested-With"] =    'XMLHttpRequest';
});
sample.controller("sampleController", function($scope, $http, $timeout, $filter) {
    $scope.studentId = "";
    $scope.picture = {};        
    $scope.uploadFile = function() {            
        var data = {studentId : $scope.studentId, picture: $scope.picture };            
        var response = $http.post('${pageContext.servletContext.contextPath}/sampletest', data);
        response.success(function(data, status, headers, config) {
            alert("jj");
        });
        response.error(function(data, status, headers, config) {
            alert("Exception details: " + JSON.stringify({ data : data }));
        });
    };
});
</script>
<body>
<div ng-app="sample" ng-controller="sampleController">
    <input type="text" ng-model="studentId">
    <input type="file" ng-model="picture">
    <button ng-click="uploadFile">Submit</button>
</div>

My Multipart configuration is :

public MultipartResolver multipartResolver(){
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(1000000);
    return multipartResolver;

}

@Bean
public MultipartConfigElement multipartConfigElement(){
    return new MultipartConfigElement("");

}

ERROR : The request sent by client is syntactically incorrect

4
  • 1
    There is no file upload directive in your example. Uploading files with AJAX is a bit more complex and doesn't work with IE < 10. Since you are a beginner I strongly suggest to use an existing solution. Commented Apr 13, 2015 at 11:40
  • hello sir, i did searched for solutions but they are complex. can u please suggest or give link to some simple solutions. Just need to upload an image through ajax post method.
    – Kharoud
    Commented Apr 13, 2015 at 20:37
  • What about this one? Looks quite simple. Just try any one and if you encounter a problem you can ask a new question. Commented Apr 13, 2015 at 21:54
  • Try to make it work with plain HTML to the server first. Then try to make Angular work. Use Chrome Developer tools and verify your HTTP traffic and JavaScript console. Commented Apr 14, 2015 at 17:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.