ivantxo
ivantxo

Reputation: 639

Yii file upload with Ajax call?

Hello I'm doing file upload with Yii. I have implemented this way and it works very nice:

View:

echo CHtml::form($this->createUrl('uploadreport'), 'post', array('enctype'=>'multipart/form-data'));
<div id="div_upload" class="row" style="display:none">
    <?php
    $this->widget('CMultiFileUpload',array(
        'name'        => 'files',
        'accept'      => 'doc|docx',
        'max'         => 1,
        'htmlOptions' => array('size' => 25),
    ));

    echo CHtml::submitButton('Upload');
    ?>
</div>

Controller:

if (isset($_FILES['files'])) {
    $tmp_name = $_FILES['files']['tmp_name'][0];
    $filename = $_FILES['files']['name'][0];
    $new_url = '/home/ivan/reports/'.$filename;

    // Upload file
    move_uploaded_file($tmp_name, $new_url);

    $calc_id = $_POST['calc_id'];

    // Check if there is a previous report
    $report = Report::model()->findByAttributes(array('calc_id'=>$calc_id));
    if (isset($report)) {
        // If there is a previous report, delete it
        $qtxt = "DELETE FROM `tbl_reports` WHERE `calc_id` LIKE '$calc_id';";
        $exec = Yii::app()->db->createCommand($qtxt)->execute();
    }

    // Insert the new report
    $rep_add = new Report;
    $rep_add->calc_id = $calc_id;
    $rep_add->report_url = $new_url;
    $rep_add->save();
}

As you can see in my view I am using CHtml::submitButton. But I would like to use this instead:

echo CHtml::button(
    'Upload Report', array(
        'ajax'    => array(
            'type' => 'POST',
            'url'  => CController::createUrl('Calculator/uploadReport'),
        )
    )
);

But if I use the above, then in my controller $_FILES['files'] comes empty or if I try CUploadedFile::getInstance(); it returns an empty string.

I want to use ajax because I don't want to render any other view I want the user to remain in the current view after he/she uploads the file. How can I use Ajax or how can I stop the refreshing of the current view when I use CHtml::submitButton('Upload');

Any hints/help appreciated.

Upvotes: 0

Views: 10069

Answers (4)

christian
christian

Reputation: 558

why didn't you use this: http://www.yiiframework.com/extension/coco/ it is very powerfull, hope it helps you

Upvotes: 0

ivantxo
ivantxo

Reputation: 639

I ended up using EAjaxUpload extension. I think it is not possible to handle file uploads through Ajax in Yii.

Upvotes: 1

ivantxo
ivantxo

Reputation: 639

I am not able to call my controller action through an ajax action. So, I think it is not possible in Yii. I ended up using this extension: http://www.yiiframework.com/extension/eajaxupload/, which performs File uploads with Ajax.

Upvotes: 0

Alfredo Castaneda Garcia
Alfredo Castaneda Garcia

Reputation: 1659

Use CHtml::ajaxSubmitButton() instead. That's the correct way to perform ajax submission. Check the documentation here: http://www.yiiframework.com/doc/api/1.1/CHtml/#ajaxSubmitButton-detail instead

Upvotes: 0

Related Questions