如何使用cjuidialog小部件上传文件?

时间:2012-09-12 18:26:04

标签: php yii yii-extensions cjuidialog

问:如何使用cjuidialog小部件上传文件?

description:我想用弹出框上传一个文件。所以我选择了cjuidialog框小部件。我跟随here。但我面临着保存和验证。提交后,没有保存数据,当验证失败时,弹出框会一次又一次地重新显示(重复)。

这是views / aa / _form.php

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

这是controller / aacontroller.php

public function actionAttachmentForm()
    {
        $media=new Media;
        $this->performAjaxValidation($media);
        $flag=true;
        if(isset($_POST['Media']))
        {       
            $flag=false;
            $media->attributes=$_POST['Media'];


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

            var_dump($media->attributes);

            if($media->save()) {
                //do something here and renderPartial to uploadedfile.php to show uploaded files.
                $this->renderPartial('uploadedFile','',false,true);
            }
        }

        if($flag == true) {
            Yii::app()->clientScript->scriptMap['jquery.js'] = false;

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

    }

这是views / aa / uploadform.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');

这是views / 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'),
)); 
//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>

1 个答案:

答案 0 :(得分:0)

我推荐 'EAjaxUpload' 扩展程序。 它非常容易使用,如果您有任何问题我可以提供帮助,因为我实施了它。