문제

Am using CJuiDialog widget with ajaxlink for popup form..it works well..

But now i need to upload multiple files from that popup form.. so i use CMultiFileUpload for upload option but i acts like a normal single file button.. please help me to solve this.. Here is my Code:

 <?php echo CHtml::ajaxLink(Yii::t('image','Upload'),array('gallery/create'),array(
    'success'=>'js:function(data){
    $("#gallery-form").dialog("open");
    document.getElementById("add_images").innerHTML=data;}'));
$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
            'id'=>'gallery-form',
            'options'=>array(
                'title'=>Yii::t('image','Upload'),
                'autoOpen'=>false,
                'model'=>'true',
                'width'=>'auto',
                'height'=>'auto',
            ),
            ));
echo "<div id='add_images'></div>";
$this->endWidget('zii.widgets.jui.CJuiDialog');  ?>

and this is my controller action:

            if(isset($_FILES['image']))
    {
        $model->attributes=$_POST['Photo'];
        $images = CUploadedFile::getInstancesByName('image');
        if(isset($images) && count($images)> 0) 
        {
            foreach ($images as $image=>$pic) 
            {
                               if (!is_dir(Yii::getPathOfAlias('webroot').'/gallery/'.$user->username===true) ){
                                   mkdir(Yii::getPathOfAlias('webroot').'/gallery/'.$user->username, 0777);
            }


                            if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/gallery/'.$user->username.'/'.$pic->name))    
                {   
                    $model->setIsNewRecord(true);
                    $model->id = null;
                                $model->image = $pic->name;
                                            $model->setAttribute('user_id',$id);
                                $model->save();
                }               
            }
                    $this->redirect(array('admin','id'=>$model->id));
        }
    }
 Ajax Popup Request
                if( Yii::app()->request->isAjaxRequest )
                             {
                    Yii::app()->clientScript->scriptMap['jquery.js'] = false;
                    $this->renderPartial('create',array(
                                        'model'=>$model,
                                ));}else{}

This is my rendered partial view file which works fine with multiupload file without call with cjuidialog but when i call it with cjuidialog it just support only single file upload..Please help.

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'gallery-form',
'enableAjaxValidation'=>true,
 'htmlOptions' => array(
        'enctype' => 'multipart/form-data',),
)); ?>

<?php echo $form->errorSummary($model); ?>
<div class="row">
    <?php echo $form->labelEx($model,'image'); ?>
     <?php
      $this->widget('CMultiFileUpload', array(
         'model'=>$model,
         'name'=>'image',
         'attribute'=>'image',
         'accept'=>'jpg|gif|png',

      ));
    ?>

    <?php echo $form->error($model,'image'); ?>
</div>

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Upload' : 'Save'); ?>
</div>

<?php $this->endWidget(); ?>
도움이 되었습니까?

해결책

renderPartial doesn't load scripts by default and the CMultiFileUpload widget uses scripts, so you need to make the renderPartial load scripts. You can do that by changing your renderPartial to include two extra parameters:

$this->renderPartial('viewName', 
    array('variable' => $variable), false, true
); 

Notice the false, true added to the end? The false means "don't return the view, display it" and the true means "postprocess," which is what will add the scripts.

For more info: http://www.yiiframework.com/doc/api/1.1/CController#renderPartial-detail

[edit]

Here's a different way of loading the dialog. It still requires the "false, true" in renderPartial:

Yii::app()->clientScript->registerScript('uploadDialog', "
$(function(){
    $('#upload-image').click(function(){
        $('#gallery-form').load('".Yii::app()->createUrl('gallery/create')."', function(){
            $('#gallery-form').dialog('open');
        });
        return false;
    });
});");

echo CHtml::link('Upload', '#', array('id' => 'upload-image'));

$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
    'id'=>'gallery-form',
    'options'=>array(
        'title'=>Yii::t('image','Upload'),
        'autoOpen'=>false,
        'model'=>'true',
        'width'=>'auto',
        'height'=>'auto',
    ),
));

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

Then, remove the line below from your controller:

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

If that still doesn't work, I would recommend adding your renderPartial directly in between the CJuiDialog widget tags instead of loading the content via ajax.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top