How To Upload A File With Cjuidialog

How to upload a file with CJuiDialog?

problem 1 : validation is working. but when the validating was false, the first popup box was not close and the another popup box was display with error.

problem 2 : can’t get the file name at actionAttachmentform().

This is the view aa/create.php


  <div class="row">

    <div id="uploadedfile" style="margin-bottom:10px;"></div>

    <?php

    echo CHtml::ajaxLink(Yii::t('attachment','Attachment'),$this->createUrl('attachmentform'),array(

        'onclick'=>'$("#attachDialog").dialog("open"); return false;',

        'update'=>'#attachDialog'

        ),array('id'=>'showattachDialog', 'class'=>'btn btn-info'));

	?>

    </div>

    

    <div id="attachDialog"></div>

this is the controller aacontroller.php


public function actionAttachmentForm()

	{

		$media=new Media;

        $this->performAjaxValidation($media);

        $flag=true;

        if(isset($_POST['Media']))

        {       

			$flag=false;

          	if($media->validate()) {

				$this->create_temp_dir($media);

				$this->renderPartial('uploadedFile','',false,true);

			}

			echo 'debug';

		}

		

		if($flag == true) {

			Yii::app()->clientScript->scriptMap['jquery.js'] = false;

			$this->renderPartial('uploadform',array('model'=>$media,),false,true);

	        }

		

		

	}

protected function create_temp_dir($model)

	{

		$sessions = new CHttpSession();

        

        // run getSessionId, this seems to trigger garbage collection...

        if(!is_dir(Yii::getPathOfAlias('webroot').'/images/temp/'. Yii::app()->user->id )) {

			mkdir(Yii::getPathOfAlias('webroot').'/images/temp/'. Yii::app()->user->id );

			chmod(Yii::getPathOfAlias('webroot').'/images/temp/'. Yii::app()->user->id, 0755); 

			// the default implementation makes it under 777 permission, which you could possibly change recursively before deployment, but here's less of a headache in case you don't

		}	

		// Check the dir is exists. If the dir $model->id is not exists, create new one.

		if(!is_dir(Yii::getPathOfAlias('webroot').'/images/temp/'. Yii::app()->user->id . "/" . $sessions->getSessionId())) {

			mkdir(Yii::getPathOfAlias('webroot').'/images/temp/'. Yii::app()->user->id . "/" . $sessions->getSessionId());

			chmod(Yii::getPathOfAlias('webroot').'/images/temp/'. Yii::app()->user->id . "/" . $sessions->getSessionId(), 0755); 

			// the default implementation makes it under 777 permission, which you could possibly change recursively before deployment, but here's less of a headache in case you don't

		}	

		

		

		

		

		// Then we save the new file	

		$model->name = CUploadedFile::getInstance($model,'name');	

		$model->name->saveAs(Yii::getPathOfAlias('webroot').'/images/temp/'. Yii::app()->user->id . "/" . $sessions->getSessionId() . "/" .  $model->name);

		$model->name = date('Y-m-d-H-i-s') ."-". $model->name;

		

		$model->save();

	}




this is view/aa/uploadform.php


<?php 

$this->beginWidget('zii.widgets.jui.CJuiDialog',array(

                'id'=>'attachDialog',

                'options'=>array(

                    'title'=>Yii::t('attachment','Attachment Form'),

                    'autoOpen'=>true,

                    'modal'=>'true',

                    'width'=>'450',

                    'height'=>'300',

					'draggable' => false,

					'resizable'=> false,

                ),

                ));

echo $this->renderPartial('_formupload', array('model'=>$model)); ?>

<?php $this->endWidget('zii.widgets.jui.CJuiDialog');?>

this is view/aa/_formupload.php


<div class="form" id="attachDialogForm">

 

<?php $form=$this->beginWidget('CActiveForm', array(

    'id'=>'attach-form',

    'enableAjaxValidation'=>true,

	'htmlOptions' => array('enctype' => 'multipart/form-data'),

	'clientOptions'=>array('validateOnSubmit'=>TRUE),

)); 

//I have enableAjaxValidation set to true so i can validate on the fly the

?>

 

    <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,'name'); ?>

        <?php echo $form->fileField($model,'name'); ?>

        <?php echo $form->error($model,'name'); ?>

    </div>

    

    <div class="row">

        <?php echo $form->labelEx($model,'description'); ?>

        <?php echo $form->textField($model,'description'); ?>

        <?php echo $form->error($model,'description'); ?>

    </div>

    

 

    <div class="row buttons">

        <?php 

		echo CHtml::ajaxSubmitButton(Yii::t('attachment','Upload'),

			CHtml::normalizeUrl(array('attachmentform','render'=>false)),array(

			//'beforeSend' => 'function(){$("#uploadedfile").addClass("loading");}',

			//'complete' => 'function(){$("#uploadedfile").removeClass("loading"); $("#attachDialog").dialog("close");}',

			'success'=>'js: function(data) {

				//alert(data);

			$("#attachDialog").dialog("close");

			$("#uploadedfile").html(data);

		}'),array('id'=>'closeattachDialog')); 

		?>

    </div>

 

<?php $this->endWidget(); ?>

 

</div>