1

I have a form which is created using cakephp form builder, this form has a couple of file upload fields as below:

<?php echo $this->Form->create('application', array('type' => 'file', 'url' => array('app' => true, 'controller' => 'jobs', 'action' => 'save_new_application'), 'id' => 'create-application-form'));

    echo '<p>'.$this->Form->input('cv', array('type' => 'file', 'label' => "Upload CV (Required)", 'required' => true)).'</p>';
    echo '<p>'.$this->Form->input('cover-letter', array('type' => 'file', 'label' => "Upload Cover Letter (optional)")).'</p>';
    echo $this->Form->input('campaignid', array('type' => 'hidden', 'class' => 'form-control sendid', 'label' => false,  'value' => $campaignid));
    echo $this->Form->input('profileid', array('type' => 'hidden', 'class' => 'form-control sendid', 'label' => false,  'value' => $profileid));
    echo $this->Form->submit('Apply', array('class' => 'form-control')); ?>

<?php echo $this->Form->end(); ?>

I however need this form to be submitted via ajax so that the page doesnt refresh, as with other forms on the site I have the below jquery, however the controller only gets the two hidden fields and no information about the upload files.

$('#create-application-form').off().on('submit', function(e){
            e.preventDefault();
            $.ajax({
                url: '/app/jobs/save_new_application/',
                dataType: 'json',
                method: 'post',
                data:  $(this).serialize()
            })
                .done(function(response) {
                    //show result
                    if (response.status == 'OK') {

                    } else if (response.status == 'FAIL') {

                    } else {
                        //show default message
                    }
                })
                .fail(function(jqXHR) {
                    if (jqXHR.status == 403) {
                        window.location = '/';
                    } else {
                        console.log(jqXHR);

                    }
                });

        });

What do i need to change in the ajax call to be able to pass the actual file data to the controller so the files can be saved on the server?

DevWithZachary
  • 3,545
  • 11
  • 49
  • 101
  • possible duplicate of [How can I upload files asynchronously?](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously) – ndm Jul 21 '15 at 15:37

1 Answers1

2

You have to send New FormData() object to send file using ajax

Updated code

   $('#create-application-form').off().on('submit', function(e){
        e.preventDefault();
        var formdatas = new FormData($('#create-application-form')[0]);
        $.ajax({
            url: '/app/jobs/save_new_application/',
            dataType: 'json',
            method: 'post',
            data:  formdatas,
            contentType: false,
            processData: false
        })
            .done(function(response) {
                //show result
                if (response.status == 'OK') {

                } else if (response.status == 'FAIL') {

                } else {
                    //show default message
                }
            })
            .fail(function(jqXHR) {
                if (jqXHR.status == 403) {
                    window.location = '/';
                } else {
                    console.log(jqXHR);

                }
            });

    });
Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46