2

I am using cake PHP file upload with the AJAX form submit. But it is not wotrking. There is nothing in the $_FILES array. But When I use normal form submit, it is working fine without any issue. I am using the following code for form submit using AJAX

    echo $this->Form->create('AlbumDetail',array('enctype' => 'multipart/form-data','type'=>'file'));

    echo $form->input('Image',array("type" => "file"));  

    echo $ajax->submit('Add Album', array('url'=> array('controller'=>'album_details', 'action'=>'addalbum'), 'update' => 'album_update'));

While ,

echo $this->Form->create('AlbumDetail', array('enctype' => 'multipart/form-data', 'controller' => 'album_details', 'action' => 'addalbum', 'type' => 'file'));

echo $form->input('Image',array("type" => "file")); 

echo "<input name='submit' type='submit' value='Add Album'>";

is working without any problem and $_FILES array is returning values. Can anyone do a little help ...?

Balaji Kandasamy
  • 4,446
  • 10
  • 40
  • 58
Happy Coder
  • 4,255
  • 13
  • 75
  • 152

3 Answers3

2

As mentioned by void0, you cannot post a file using Ajax. This similar question has some workarounds and suggested libraries.

Community
  • 1
  • 1
RichardAtHome
  • 4,293
  • 4
  • 20
  • 29
  • 1
    you know any cakephp plugin or something that can be utilized with the forms so that the integration will be much for easy – Happy Coder Jun 11 '12 at 09:55
  • I've had some success using the jQuery uplodify plugin ( http://www.uploadify.com/ ) . Warning - it DOES work, but it's a real pain to debug the flash stuff :-( – RichardAtHome Jun 12 '12 at 16:29
  • I have done this using a hidden iframe. Seems like it is pretty easy than other stuffs. So this will show validation also without using AJAX – Happy Coder Jun 12 '12 at 16:50
0

Have a look at this CakePHP plugin: https://github.com/srs81/CakePHP-AjaxMultiUpload

I think this may be exactly what you are looking for.

Suman
  • 9,221
  • 5
  • 49
  • 62
0

It is possible now.

This is how I accomplished that. First of all I'm using Uploadable Behavior for handling files uploading from: https://github.com/cakemanager/cakephp-utils

Model:

$this->addBehavior('Utils.Uploadable', [
       'file' => [
       'removeFileOnUpdate' => false,
       'field' => 'file_tmp',
       'path' => dirname(ROOT).'{DS}invoices{DS}', 
       'fileName' => '{field}'
       ]
    ]);

Controller:

public function ajaxInvoice() {

    if ($this->request->is('ajax')) {
        $this->autoRender = false;
        $this->Invoices->deleteAll(['order_id' => $this->request->data['order_id']]);

        $invoice = $this->Invoices->newEntity();

        $invoice->order_id = $this->request->data['order_id'];
        $invoice->file_tmp = $this->request->data['file']['name'];

        $invoice = $this->Invoices->patchEntity($invoice, $this->request->getData());

        $this->Invoices->save($invoice);
        $this->response->body($invoice);

    }
}

Template:

<?php use Cake\Routing\Router; ?>

<input type="file" class="upload<?= $id ?> hidden"><a data-id="<?= $id ?>" class="upload">Upload</a>

<script type = "text/javascript" > $(document).ready(function() {
    $('.upload').on('click', function() {
        var id = $(this).attr('data-id');
        $('.upload' + id + '').click();
        $('.upload' + id + '').change(function(e) {
            e.stopImmediatePropagation();
            var form = new FormData();
            form.append('file', $(this)[0].files[0]);
            form.append('order_id', id);
            $.ajax({
                type: "POST",
                url: '<?php echo Router::url(array('controller ' => 'Invoices ', 'action ' => 'ajaxInvoice ')); ?>',
                data: form,
                cache: false,
                contentType: false,
                processData: false,
                success: function(data, status, xhr) {
                    var response = JSON.parse(xhr.responseText);
                },
            });
        });
    });
}); 
</script>
danny3b
  • 313
  • 5
  • 17