w454638 2011-05-13 11:43:17 4337次浏览 6条回复 0 0 0

写了一个简单的yii上传功能,测试不成功,来请教下; 模型里面是这样写的

public function rules()
{
  return array(          array('address_avatar','file','types'=>'jpg,gif,png','maxSize'=>'1024*1024*1','tooLarge'=>'图片大小超过1M,请从新上传。'),
  );
}

控制器里面是这样写的

public function actionindex()
{
  $model = Users::model()->findByPk(1);	
  if(isset($_POST['Users']))
  {
    $model->attributes=$_POST['Users'];
    $image=CUploadedFile::getInstance($model,'address_avatar');
    if($model->address_avatar)
    {
      $newName = time().'.'.$model->address_avatar->extensionName; 
    }
    if($model->save())
    {
      $newName = $model->address_avatar->saveAs('assets/avatar/');
      $model->image->saveAs('assets/avatar/'.$newName);
      $model->address_avatar = $newName;
      echo "ok";
    }
    exit;
   }
   $this->render("index",array('model'=>$model));
}

视图里面这样写的

<?php 
$form=$this->beginWidget('CActiveForm', array(
  'id'=>'avatar-form',
  'enableAjaxValidation'=>true,
  'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>
<div class="row">
<?php //echo $form->labelEx($model,'address_avatar'); ?>
<?php echo CHtml::activeFileField($model,'address_avatar'); ?>
<?php echo $form->error($model,'address_avatar'); ?>
</div>

<div class="row">
<?php echo '图片预览'; ?>
<?php echo '<img src="'.$model->address_avatar.'" border="0"/>'; ?>
</div>
<?php echo CHtml::submitButton('提交'); ?>
<?php $this->endWidget(); ?>

怎么图片就是上传不了呢? 求解。。

您需要登录后才可以回复。登录 | 立即注册