0

I hava a java script function in my JSP page

    function submitProductCategoryForm()
    {
        document.getElementById('isSave').value="1";
        var elements=document.getElementById('addProductCategoryFrom').elements;
        var url = "addProductCategory.jsp?";
       for (var i = 0; i < elements.length; i++)
          url += elements[i].id + "=" + encodeURIComponent($("#" + elements[i].id).val()) + "&";
       $.post(url, function(data)
       {
            alert("Function Complete");
       }); 
   }

It call a jsp page and here I saved the data in DB.

Now on this form there is a file input is also there, so it must be to set the content type to multipart/form-data

but when i call this function i found the error that

java.io.IOException: Posted content type isn't multipart/form-data

my form is like this

<form action="addProductCategory.jsp" method="post" enctype="multipart/form-data" id="addProductCategoryFrom">

So please how i set the content type to multipart.

thanks in advance

DEVANG SHARMA
  • 2,572
  • 4
  • 30
  • 49
  • 2
    this has been answered already. You can't use Ajax to submit a file. [link](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery) [link](http://stackoverflow.com/questions/4856917/jquery-upload-progress-and-ajax-file-upload) – Pablo Mescher May 24 '12 at 04:45
  • 1
    if you want to upload files in an Ajax fashion, you can use something like Uploadify - it is not supported natively. – Joe Bowman May 24 '12 at 06:42

1 Answers1

0

I used a FormData object and it seems to do what you want, check my question/solution https://stackoverflow.com/a/21191491/995514

$("#theForm").submit(function(e){
    e.preventDefault();
    var theForm = new FormData($(this)[0]);
    $.ajax({
        url: '.../rest/save',
        type: 'POST',
        data: theForm,
        cache, false,
        contentType: false,
        processData: false
    });
    return false;
});
Community
  • 1
  • 1
Half_Duplex
  • 3,653
  • 3
  • 24
  • 46