3

The following code does not post the file data to the server in IE9.

FormData() object looks to be the issue but not sure how to resolve it.

I used form.serialize() but this is not uploading the file.

Im hesitant to implement a JQuery file uploader just for this function. Is there a simple way to upload a file similar to FormData() ?

    // HTML
    <form name='aform' action='upload.php'>
           <input type='file' name='afile'>
           <input type='text' name='qty' value='1'>
           <input type='hidden' name='product_id' value='7'>
           <a class='addToCartButton'>Add to cart</a>
    </form>



    // JS
    $(document).on('click', '.addToCartButton', function(event) 
    {

        var form_id = $(this).closest('form').attr('id');

        var formElement = document.getElementById(form_id);

        var odata = new FormData(formElement);        

        //var $form = $('#'+form_id);

        $.ajax({
            url: 'http://localhost/cart/add',
            data: odata, //$form.serialize(),
            type: 'POST',
            processData: false,  // tell jQuery not to process the data
            contentType: false   // tell jQuery not to set contentType            
        }).done(function(data) 
        {

            var returnObject = jQuery.parseJSON(data);

            switch(returnObject.status) {
                case 'success':
                    alert('Item added to cart');
                    break;              
                case 'error':
                    alert('An error occured');
                    break;
                case 'no_file':
                    alert('No file was detected');
                    break;                                      
            }                           

        });

        event.preventDefault();
    }); 
user1827093
  • 87
  • 3
  • 11
  • http://caniuse.com/#search=formdata – Milche Patern May 26 '15 at 23:56
  • Yes, I found that IE9 was unsupported, but how would I upload files using jQ/ajax without FormData ? I have seen fileupload libs but I also want to add other form items not just files. Most libs I see are just uploading a file. I want to be able to post a file[s] and other form data in a single request – user1827093 May 27 '15 at 00:11

1 Answers1

0

It's because IE9 does not support it.

http://caniuse.com/#search=formdata

Fully related : Post formdata via XMLHttpRequest object in JS ? ( cross browser)

Community
  • 1
  • 1
Milche Patern
  • 19,632
  • 6
  • 35
  • 52