0

I'm retrieving my form data and submitting it to the server using ajax. The file is not available. The other field data is available.

$('#apply-form').submit(function(e){

    var formData = new FormData($(this)[0]);

      $.ajax({
            url: '/public/listener?CO=ATV',
            type: 'POST',
            dataType: 'json',
            data: formData,
            success: function (data) {
                console.log(data);
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;


});

This is how i access the request on the server side

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);

List<Object> list = upload.parseRequest(this.request);

if(list != null){

    for(Object fileItem : list){

        FileItem item = (FileItem)fileItem;                 
        if(! (item.isFormField()) )
                this.item =item;            
    }   
}

Any clue why the file is null ? Maybe i'm accessing it wrong, if so can you show me the correct way.

lego.warrior
  • 342
  • 3
  • 16

1 Answers1

0

You can't post files with jquery. Futhermore I see no reason to do this in your case. Just change client side to:

 <form id="apply-form" action="/public/listener?CO=ATV" method="post" enctype="multipart/form-data">
 your form
</form>

And post your form:

$( "#other" ).click(function() {
   $('#apply-form').submit(function( event ) {
    console.log(event);
   });
});

note: if you don't prevent the default event The form is being posted by browser.

edit Actually with html5 you can do this. Check this post

Community
  • 1
  • 1
deathangel908
  • 8,601
  • 8
  • 47
  • 81