0

我正在尝试在 CjuiDialog 小部件中上传文件,我正在 Controller 类中打印上传的文件名但它返回 null,我试图打印 $POST 数组,图像数组返回空,请参见下面的代码,

我的控制器代码,

 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"));
            }

        }

我的查看代码,

<?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(); ?>
4

1 回答 1

0

在你的模型中试试这个

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'),
);
}

在您的控制器中

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));
        }
于 2013-12-27T07:24:12.483 回答