php - Upload Image with Yii -
i have problem file upload in yii1 ..
this 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 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 error when try save data. 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 /> </span><span style="color: #dd0000">'profile' </span><span style="color: #007700">=> array<br /> (<br /> </span><span style="color: #dd0000">'0' </span><span style="color: #007700">=> </span><span style="color: #dd0000">'profile cannot be blank.'<br /> '1' </span><span style="color: #007700">=> </span><span style="color: #dd0000">'profile cannot be blank.'<br /> </span><span style="color: #007700">)<br />)</span> </span> </code> syntaxerror: json.parse: unexpected non-whitespace character after json data @ line 1 column 33 of json data ...ction(b){if(a.json&&a.json.parse)return a.json.parse(b+"");var c,d=null,e=m.trim...
and have done additional rules in model :
array('profile', 'file','types'=>'jpg, gif, png'),
anybody ever experience similar problem ??
die(cvardumper::dump($model->errors,10,true));
do need more?
anyway, here's full answer:
this:
{"status":false,"pesan":"gagal"}<code><span style="color: #000000"> <span style="color: #0000bb"></span><span style="color: #007700">array<br />(<br /> </span><span style="color: #dd0000">'profile' </span><span style="color: #007700">=> array<br /> (<br /> </span><span style="color: #dd0000">'0' </span><span style="color: #007700">=> </span><span style="color: #dd0000">'profile cannot be blank.'<br /> '1' </span><span style="color: #007700">=> </span><span style="color: #dd0000">'profile cannot be blank.'<br /> </span><span style="color: #007700">)<br />)</span>
is not json. not valid json anyway.
javascript on page tries parse json, , hence fails.
so, html garbage coming from, breaks json?
have no way of verifying line does, i'm pretty sure dumps stuff syntax highlighting:
die(cvardumper::dump($model->errors,10,true));
in other words, remove it.
Comments
Post a Comment