thura747
(Thura747)
July 18, 2012, 10:32am
1
Hi there,
I would like to delete an uploaded image at update view and replace the file upload.
this view
<div class="row" id="file_upload">
<?php
$file = 'images/'. $model->id . '/'. $model->file_path;
if (file_exists($file)){
echo '<img src="'.$file.'" width="150px" />' ;
echo CHtml::ajaxLink('remove', array('ajax'), array('update'=>'#forAjaxRefresh'));
}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>
This is controller
public function actionAjax()
{
$model=new CompanyLicense('search');
$this->render('update', array('model'=>$model), false, true);
}
Actually It is not working. when I click the remove, it is showing the update page at div tag. Please help me.
perochak
(Amjad Mughal)
July 18, 2012, 10:42am
2
Your code
public function actionAjax()
{
$model=new CompanyLicense('search');
$this->render('update', array('model'=>$model), false, true);
}
says that it should rander update view, you should code your delete image code so that image will be deleted and appropriate message which you want to display on delete.
thura747
(Thura747)
July 19, 2012, 12:15pm
3
PeRoChAk:
Your code
public function actionAjax()
{
$model=new CompanyLicense('search');
$this->render('update', array('model'=>$model), false, true);
}
says that it should rander update view, you should code your delete image code so that image will be deleted and appropriate message which you want to display on delete.
Thank you for your reply. but I am very newbie for Yii. Please tell me more detail.
perochak
(Amjad Mughal)
July 22, 2012, 1:41am
4
When you click on link generated by code
echo CHtml::ajaxLink('remove', array('ajax'), array('update'=>'#forAjaxRefresh'));
Then it calls ajax action. This action will, currently, display the results of
$model=new CompanyLicense('search');
$this->render('update', array('model'=>$model), false, true);
and does not delete the object/image and the results are updated in div/object with id forAjaxRefresh.
So, you need to first unlink the image or object and then display the remaining data i.e. list of images.
To delete an object/image, you should now its name value etc. So, in your code to generate the button/link to delete image, you need to provide its representative i.e. id as
echo CHtml::ajaxLink('remove', array('ajax',array('id'=>$model->id)), array('update'=>'#forAjaxRefresh'));
You can then use it in your ajax function and load the object as
$model=Company::model()->findByPk($_GET['id']);
if($model){
unlink(full path of image);
}
thura747
(Thura747)
July 23, 2012, 5:51am
5
PeRoChAk
I already got error.
"Your request is invalid."
this is ajax
http://localhost/mmaig_ceo/ceo-control-system/index.php?r=companylicense/ajax&0[id]=1&_=1343021719536
This is controller
public function actionAjax($id)
{
$model=Company::model()->findByPk($_GET['id']);
if($model){
$file = '/images/'. $model->id . '/'. $model->file_path;
unlink(dirname(__FILE__) . '/../../..'.$file);
}
}
I put the Ajax at accessRules(). Please help me.
thura747
(Thura747)
July 23, 2012, 6:21am
6
I changed the ajaxlink to this one.
<?php echo CHtml::ajaxLink('remove2', array('url'=>array('companylicense/ajax', 'id'=>$model->id),
), array('update'=>'#forAjaxRefresh')); ?>
At that time, ajax is working correctly. and returning the current update.php to the div tag (#forAjaxRefresh ). Actually, I want to replace the upload filed after deleting the img. How can I do that?
Thx
perochak
(Amjad Mughal)
July 23, 2012, 6:44am
7
At that time, ajax is working correctly. and returning the current update.php to the div tag (#forAjaxRefresh ). Actually, I want to replace the upload filed after deleting the img. How can I do that?
Can you give me a snapshot of your app page so that I can understand it more…
thura747
(Thura747)
July 23, 2012, 9:35am
8
PeRoChAk, this is the snapshots
Before clicking the remove2 link
After clicking the remove2 link
thura747
(Thura747)
July 24, 2012, 5:06am
10
I can solved it.
Model nothing change
View (_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>
view (uploadfield.php)
<?php echo CHtml::label('File', 'Application_word_report'); ?>
<?php echo CHtml::fileField('CompanyLicense[file_path]', ''); ?>
Controller
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->expire_date= $_POST['expire_date'];
$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,
));
}