1

Is there a way to submit “multipart/form-data” forms with CakePHP + Ajax + jQuery ?

When I submit the form using Ajax in my CakePHP application, I get all data from the form but not the file that I choose to upload using file control.

This is my code:

    function save_option(id)
    {
       $.ajax({

            type        : "post",
            url         : site_url + "lists/save_option/",              
            data        : $("#option_form_"+id).serialize(),
            success     : function(response) {              
                //on success
            },
            error       : function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }

       });

    }

Please help me out.

Thanks

1 Answer 1

2

You can't submit files via ajax (generally) due to security restrictions.

However, you can use JS in conjunction with an iframe to get a similar effect. See here for a tutorial : http://www.openjs.com/articles/ajax/ajax_file_upload/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a ton for the link to a solution...I was suspicious about this initially but now I am sure that submitting files via ajax is not possible. Another solution that I came across - atwebresults.com/php_ajax_image_upload - though it basically uses same concept of iframe as you pointed out.

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.