Upload Image with Yii

I have problem with file upload in Yii1 ..

this my controller

<?php

class SiteController extends Controller
{
    public function actionIndex()
    {
        $this->render('upload', array('model'=>new User));
    }

    public function actionSimpan() {
        $model = new User;
        //$direc = Yii::app()->basePath.'\images\\';
        if(isset($_POST)) {
        //if(Yii::app()->request->isPostRequest) {
            $random = rand(0, 999);
            $uploadedFile = CUploadedFile::getInstance($model, 'profile');
            $fileName = "{$random}-{$uploadedFile}";
            $model->profile = $fileName;
            //$model->profile=CUploadedFile::getInstanceByName('profile');
            //print_r($direc); die();
            if($model->save()) {
                $uploadedFile->saveAs(Yii::app()->basePath.'\images\\'.$model->profile.'.jpg');
                echo CJSON::encode(array('status'=>true, 'pesan'=>'Berhasil'));
            } else {
                echo CJSON::encode(array('status'=>false, 'pesan'=>'Gagal'));
                die(CVarDumper::dump($model->errors,10,true));
            }
        }
    }

}

and this my view

<?php 
    $form=$this->beginWidget('CActiveForm', array(
        'id'=>'form-upload',
        'htmlOptions'=>array('enctype'=>'multipart/form-data', ),
    ));
?>

<div class="row">
    <?php echo $form->labelEx($model, 'profile'); ?>
    <?php echo $form->fileField($model, 'profile'); ?>
</div>

<div class="row">
    <?php echo CHtml::Button('Simpan', array('onclick'=>'simpan()')); ?>
</div>

<?php $this->endWidget(); ?>

<script type="text/javascript">
    function simpan() {
        var data = $('#form-upload').serialize();
        $.ajax({
            type:'POST',
            url:'<?php echo Yii::app()->createUrl("site/simpan"); ?>',
            data: data,
            success: function(data) {
                var obj = jQuery.parseJSON(data);
                if(obj.status==true) {
                    alert(obj.pesan);
                } else {
                    alert(obj.pesan);
                }
            }
        });
    }
</script>

I get error when I try to save data. This error output in firebug:

{"status":false,"pesan":"Gagal"}<code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">array<br />(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'profile'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array<br />&nbsp;&nbsp;&nbsp;&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'0'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Profile&nbsp;cannot&nbsp;be&nbsp;blank.'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'1'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Profile&nbsp;cannot&nbsp;be&nbsp;blank.'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />)</span>
</span>
</code>

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 33 of the JSON data


...ction(b){if(a.JSON&&a.JSON.parse)return a.JSON.parse(b+"");var c,d=null,e=m.trim...

and I have done additional rules in Model :

array('profile', 'file','types'=>'jpg, gif, png'),

Anybody ever experience similar problem ??

Upvotes: 0

Views: 172

Answers (1)

Siguza
Siguza

Reputation: 23830

die(CVarDumper::dump($model->errors,10,true));

Do I need to say more?


Anyway, here's a full answer:

This:

{"status":false,"pesan":"Gagal"}<code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">array<br />(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'profile'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array<br />&nbsp;&nbsp;&nbsp;&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'0'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Profile&nbsp;cannot&nbsp;be&nbsp;blank.'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'1'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Profile&nbsp;cannot&nbsp;be&nbsp;blank.'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />)</span>

is not JSON. Not valid JSON anyway.
But some JavaScript on your page tries to parse it as JSON, and hence fails.
So, where is all that HTML garbage coming from, that breaks your JSON?
I have no way of verifying what this line truly does, but I'm pretty sure it dumps stuff with some syntax highlighting:

die(CVarDumper::dump($model->errors,10,true));

In other words, remove it.

Upvotes: 1

Related Questions