0

I am trying to upload file inside CjuiDialog widget, I am printing the uploaded file name in Controller class but it returns null, i tried to print $POST array, the image array returns empty, Please see below code,

My Controller Code,

 public function actionUpload()
        {
            $model=new UploadModel();
          echo var_dump($_POST);
            if(isset($_POST['UploadModel']))
            {
                $model->image=CUploadedFile::getInstance($model,'image');
echo"image-->".$model->image;
                $this->redirect(array("create"));
            }

        }

My View Code,

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'uploadfile-form',
    //'enctype'=>'multipart/form-data',
    'method'=>'post',
 'htmlOptions' => array('enctype' => 'multipart/form-data'),
    'action'=>'upload',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>false,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>
    <?php

     echo $form->errorSummary($model); ?>

        <?php echo $form->labelEx($model,'Base Name',array('text-align'=>'right','style'=>'display:inline-block'))."&nbsp;&nbsp;"; ?>
        <?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>128)); ?>
        <?php echo $form->error($model,'name'); ?>      
    <br>

        <?php echo $form->labelEx($model,'Active',array('style'=>'display:inline-block'))."&nbsp;&nbsp;"; ?>
        <?php echo $form->checkBox($model,'is_active',array('size'=>60,'maxlength'=>128)); ?>

        <?php echo $form->error($model,'is_active'); ?> 
            <br>

        <?php 

    echo $form->fileField($model, 'image');
    ?>
    <br><br>
    <?php echo CHtml::submitButton( 'Upload',array('background-style'=>'none')); ?>
    <?php echo CHtml::button('Cancel',array('background-style'=>'none','onclick'=>'$("#BCEditSource").dialog("close"); return false;')); ?>

<?php $this->endWidget(); ?>
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
US-1234
  • 1,519
  • 4
  • 24
  • 61

1 Answers1

0

try this in Your model

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
array('image','file',
                        'safe'=>true,
                        'allowEmpty'=>TRUE,
                        'maxSize'=>512000,
                        'types'=>'jpeg,pjpeg,png,gif',
                        'tooLarge'=>'The size of the file is more than 100Kb',
                        'wrongType'=>'the file must be in the jpeg,png format'),
);
}

In your controller

public function actionUpload()
        {
            $model=new UploadModel();
            if(isset($_POST['UploadModel']))
            {
                $model->image=CUploadedFile::getInstance($model,'image');
            if(!empty($model->image))
            {
               echo"image-->".$model->image;
            }
            else
           {
           echo 'no image uploaded'; 
           }
            }
     $this->render("create",array('model'=>$model));
        }
Let me see
  • 5,063
  • 9
  • 34
  • 47
  • Yes i used your code but still image not printin, FYI I have used the upload form inside CJUIDialog – US-1234 Dec 27 '13 at 07:50