YII 中的controller
public function actionUpdate($id){
$model = $this->loadModel($id);
if(isset($_POST['Shop'])){
$model->attributes = $_POST['Shop'];
$upload=CUploadedFile::getInstance($model,'uploadfile');
if($upload!=''){
$url=Upload::createFile($upload,'Shop','create');
$model->setAttribute('shop_logo', $url);
}
$model->save();
$this->redirect(array('shopmanage/update','id'=>$id));
}
$this->render('../shop/shopmanage/update',array(
'model'=>$model,
));
}
YII中的view
<div class="row">
<?php echo $form->labelEx($model,'shop_logo'); ?>:
<input type="hidden" name="Shop[uploadfile]" value="">
<input type="file" name='Shop[uploadfile]' style='display: none;' class='uploadfile'>
<input type='text' name="Shop[shop_logo]" value='<?php echo $model->shop_logo;?>' size='30'>
<input type='button' value='上传' class='buttonfile'>
<?php echo $form->error($model,'shop_logo'); ?>
</div>
YII中的js
$(document).ready(function(){
$('.buttonfile').click(function(){
$(this).parent().find('.uploadfile').click();
});
$('.uploadfile').change(function(){
$("#shopmanage-form").submit();
});
})
效果图