在你的表格中试试这个
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'imageGallery-form',
'action'=>Yii::app()->createAbsoluteUrl('/Controller/action'),
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions'=>array('enctype'=>'multipart/form-data'),// This is required for file upload
)); ?>
<div class="row">
<span class="span-4">Uplaod images:</span>
<?php echo $form->fileField($model,'Img',array('id'=>'file')); ?>
?>
</div>
添加一些脚本来验证文件字段
<script type="text/javascript">
$('#imageGallery-form').live("submit", function(event){
var success = true;
$form = $(this);
if(/.*\.(gif)|(jpeg)|(jpg)|(doc)$/.test($("#file").val().toLowerCase())){
$("#fname").removeClass("error");
}else{
$("#error_ul").append("<li>Please Choose Gif or Jpg Images Only.!!</li>");
$(".errorSummary").show();
$("#fname").addClass("error");
success = false;
}
});
</script>
并在控制器端访问图像,如图所示
public function actionGetImage(){
if(isset($_POST['imageUploadBtn'])){
$Img=CUploadedFile::getInstance($model, 'Img');
}
}