我想删除图片并在更新视图(yii)重新显示上传的字段。
$form->filefield
将显示在图片位置。这是视图
<div id="forAjaxRefresh"><img src="<?php echo $file; ?>" width="150px" /></div>
<?php echo CHtml::ajaxLink('clickMe', array('ajax'), array('update'=>'#forAjaxRefresh'));?>
这是控制器
public function actionAjax()
{
$model=new CompanyLicense('search');
//$this->render('update', array('model'=>$model,'test'=>'test'), false, true);
//echo "$form->labelEx($model,'file_path');
//$form->fileField($model,'file_path', array('class'=>'input-file'));
//$form->error($model,'file_path'); ";
//echo "$form->labelEx($model,'file_path')";
echo 'OK';
}
当我尝试的时候。它正在显示'OK'。但我想在上面显示上传文件字段。我该怎么办?请帮帮我。
=============== +++++++++++++++++++++++++++++++ = ===================================
最新的mvc
模型无变化
控制器
public function actionAjax($id)
{
$model=new CompanyLicense();
//$this->render('uploadfield', array());
//echo "$form->labelEx($model,'file_path');
//$form->fileField($model,'file_path', array('class'=>'input-file'));
//$form->error($model,'file_path'); ";
//echo "$form->labelEx($model,'file_path')";
echo $this->renderPartial('uploadfield',array('model'=>$model),false,true);
/*
$model=Company::model()->findByPk($_GET['id']);
if($model){
$file = '/images/'. $model->id . '/'. $model->file_path;
unlink(dirname(__FILE__) . '/../../..'.$file);
}
<br />
*/
}
查看(_form.php)
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'company-license-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'company_id'); ?>
<?php
$records = Company::model()->findAll();
$list = CHtml::listData($records, 'id', 'name');
?>
<?php echo $form->dropDownList($model,'company_id', $list, array('prompt'=>'Please select a Company')); ?>
<?php echo $form->error($model,'company_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'license_no'); ?>
<?php echo $form->textField($model,'license_no',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'license_no'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'license_name'); ?>
<?php echo $form->textField($model,'license_name',array('size'=>60,'maxlength'=>200)); ?>
<?php echo $form->error($model,'license_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'annualfee'); ?>
<?php echo $form->textField($model,'annualfee',array('size'=>10,'maxlength'=>10)); ?>
<?php echo $form->error($model,'annualfee'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'expire_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name'=>'expire_date',
'model'=>$model,
'value' => $model->expire_date,
'options'=>array(
'showAnim'=>'fold',
'dateFormat' => 'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>
<p> Expire Date must be greater than Current Date.</p>
<?php echo $form->error($model,'expire_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'notification_days'); ?>
<?php echo $form->textField($model,'notification_days'); ?><p> Notification Days must be between 7 and 90.</p>
<?php echo $form->error($model,'notification_days'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row" id="file_upload">
<?php
$file = '/images/'. $model->id . '/'. $model->file_path;
if (file_exists(dirname(__FILE__) . '/../../..'.$file)){
echo '<img src="http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . $file.'" width="150px" />' ;
}else{
?>
<div class="row">
<?php echo $form->labelEx($model,'file_path'); ?>
<?php echo $form->fileField($model,'file_path', array('class'=>'input-file')); ?>
<?php echo $form->error($model,'file_path'); ?>
</div>
<?php
}
?>
</div>
<div id="forAjaxRefresh"><img src="http://<?php echo $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . $file; ?>" width="150px" /></div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<?php echo CHtml::ajaxLink('remove', array('ajax'), array('update'=>'#forAjaxRefresh')); ?>
<?php
echo CHtml::ajaxLink( 'delete', array( 'companylicense/ajax', 'id'=>$model->id), array('type'=> 'GET', 'update' => '#forAjaxRefresh') );
?>
<?php echo CHtml::ajaxLink('remove2', array('url'=>array('companylicense/ajax', 'id'=>$model->id),
), array('update'=>'#forAjaxRefresh')); ?>
查看(uploadfield.php)
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'company-license-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<?php echo $form->labelEx($model,'file_path'); ?>
<?php echo $form->fileField($model,'file_path', array('class'=>'input-file')); ?>
<?php echo $form->error($model,'file_path'); ?>
<?php $this->endWidget(); ?>
请帮帮我!!!
答案 0 :(得分:1)
尽管勺子喂养某人并不健康,但看起来你有很多麻烦。看到它非常简单,你采用了复杂的方法。
按照步骤操作即可。
View
<?php echo $form->labelEx($model,'full_version'); ?>
<?php if($model->full_version == '')
echo $form->filefield($model,'full_version',array('style'=>'width:100%;'));
else
{
echo '<div id="file"><b>'.$model->full_version.'</b> '.CHtml::AjaxLink('Delete',CController::createUrl('removeFinalVersion',array('id'=>$model->id)),array('update'=>'#file')).'</div>';
}?>
<?php echo $form->error($model,'full_version'); ?>
现在我们要做的是用文件上传think替换file
Div
。如果您必须上传比文件字段更多的字段,那么您可以使用单独的文件并在控制器和回显中呈现它。但是对于这个演示,我只考虑只返回一个项目。
所以当Ajax请求发生时,事情会转到actionRemoveFinalVersion
,其中所有的谈话都需要完成并且必须返回filefield
它看起来像这样
public function actionRemoveFinalVersion($id)
{
$user = Products::model()->findByPk($id);
if($user === null)
yii::app()->end();
$user->full_version = '';
$user->save();
//return CHtml::activeFileField($model,'full_version',array('style'=>'width:100%;'));
echo CHtml::Tag('input',
array(
'style'=>"width:100%;",
'name'=>"Products[full_version]" ,
'id'=>"Products_full_version",
'type'=>"file"));
}
专注于返回的name
的{{1}}和id
。它需要精确,以便filefield
可以发生。 validation
。如果你做的更正,你也应该这样做。
另外,在使用AJAX时,请在Firefox中使用Firebug工具,在Chrome中使用类似工具
答案 1 :(得分:0)
我能解决它。 :D:D
模型没有变化
查看(_form.php)
<div class="row" id="file_upload">
<?php
if ($model->id){
$file = '/images/'. $model->id . '/'. $model->file_path;
echo '<div id="forAjaxRefresh">';
echo '<img src="http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . $file.'" width="150px" />' ;
echo CHtml::ajaxLink('remove', array('delimg', 'id'=>$model->id), array('update'=>'#forAjaxRefresh'));
echo '</div>';
}else{
?>
<div class="row">
<?php echo $form->labelEx($model,'file_path'); ?>
<?php echo $form->fileField($model,'file_path', array('class'=>'input-file')); ?>
<?php echo $form->error($model,'file_path'); ?>
</div>
<?php
}
?>
</div>
查看(uploadfield.php)
<?php echo CHtml::label('File', 'Application_word_report'); ?>
<?php echo CHtml::fileField('CompanyLicense[file_path]', ''); ?>
控制器
public function actionDelimg()
{
$model=CompanyLicense::model()->findByPk($_GET['id']);
$file = '/images/'. $model->id . '/'. $model->file_path;
if(file_exists(dirname(__FILE__) . '/../..'.$file))
unlink(dirname(__FILE__) . '/../..'.$file);
$this->renderPartial('uploadfield',array('model'=>$model),false,true);
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['CompanyLicense']))
{
$model->attributes=$_POST['CompanyLicense'];
$currentTime = date('Y-m-d h:i:s', time());
$model->updated = $currentTime;
$model->active = 1;
$model->file_path=CUploadedFile::getInstance($model,'file_path');
$folder = Yii::getPathOfAlias('webroot').'/images/';
if($model->save())
{
$model->file_path->saveAs($folder. $model->id . "/" . $model->file_path);
// redirect to success page
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('update',array(
'model'=>$model,
));
}