警予AJAX xupload表单提交不工作(yii ajax xupload form submit

2019-09-20 15:40发布

Q1:提交表单无法正常工作。

Q2:如何限制上传文件(例如1 - 只有5个文件)

状态:创建具有AJAX上传表单xupload

我的模型(fadepreciation.php)

public function afterSave( ) {
        $this->addImages( );
        parent::afterSave( );
    }

    public function addImages( ) {
        //If we have pending images
        if( Yii::app( )->user->hasState( 'images' ) ) {
            $userImages = Yii::app( )->user->getState( 'images' );
            //Resolve the final path for our images
            $path = Yii::app( )->getBasePath( )."/../images/uploads/{$this->id}/";
            //Create the folder and give permissions if it doesnt exists
            if( !is_dir( $path ) ) {
                mkdir( $path );
                chmod( $path, 0777 );
            }

            //Now lets create the corresponding models and move the files
            foreach( $userImages as $image ) {
                if( is_file( $image["path"] ) ) {
                    if( rename( $image["path"], $path.$image["filename"] ) ) {
                        chmod( $path.$image["filename"], 0777 );
                        $img = new Image( );
                        $img->size = $image["size"];
                        $img->mime = $image["mime"];
                        $img->name = $image["name"];
                        $img->source = "/images/uploads/{$this->id}/".$image["filename"];
                        $img->somemodel_id = $this->id;
                        if( !$img->save( ) ) {
                            //Its always good to log something
                            Yii::log( "Could not save Image:\n".CVarDumper::dumpAsString( 
                                $img->getErrors( ) ), CLogger::LEVEL_ERROR );
                            //this exception will rollback the transaction
                            throw new Exception( 'Could not save Image');
                        }
                    }
                } else {
                    //You can also throw an execption here to rollback the transaction
                    Yii::log( $image["path"]." is not a file", CLogger::LEVEL_WARNING );
                }
            }
            //Clear the user's session
            Yii::app( )->user->setState( 'images', null );
        }
    }

我的观点(_form.php这个)

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'fa-depreciation-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); ?>
<!-- Other Fields... -->
        <div class="row">
            <?php echo $form->labelEx($model,'photos'); ?>
            <?php
            $this->widget( 'xupload.XUpload', array(
                'url' => Yii::app( )->createUrl( "/fadepreciation/upload"),
                //our XUploadForm
                'model' => $photos,
                //We set this for the widget to be able to target our own form
                'htmlOptions' => array('id'=>'fa-depreciation-form'),
                'attribute' => 'file',
                'multiple' => true,
                //Note that we are using a custom view for our widget
                //Thats becase the default widget includes the 'form' 
                //which we don't want here
                //'formView' => 'application.views.faDepreciation._form',
                )    
            );
            ?>
        </div>
    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

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

</div><!-- form -->

我的控制器(fadepreciation.php)

public function actionCreate()
    {
        $model=new FaDepreciation;
        Yii::import( "xupload.models.XUploadForm" );
        $photos = new XUploadForm;
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['FaDepreciation']))
        {
            //Assign our safe attributes
            $model->attributes=$_POST['FaDepreciation'];
            //Start a transaction in case something goes wrong
            $transaction = Yii::app( )->db->beginTransaction( );
            try {
                //Save the model to the database
                if($model->save()){
                    $transaction->commit();
                    $this->redirect(array('view','id'=>$model->id));
                }
            } catch(Exception $e) {
                $transaction->rollback( );
                Yii::app( )->handleException( $e );
            }
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        Yii::import( "xupload.models.XUploadForm" );
        $photos = new XUploadForm;
        $this->render('create',array(
            'model'=>$model,
            'photos'=>$photos,
        ));

    }
public function actionUpload( ) // From xupload nothing change

Answer 1:

什么,你需要做的是创建一个自定义窗体。 复制从xupload _form内容,并将其粘贴去除开始形式 - 最终形式。 添加到您的窗口小部件“FormView控件”在自定义窗体的参考。



Answer 2:

什么是关于提交表单的问题?

是的文件限制可以做到的。 请确保你遵循这些http://www.yiiframework.com/wiki/348/xupload-workflow/



Answer 3:

Q1:形式submition不工作,因为XUpload小部件生成自己的表单标签。 所以你生成的HTML有另一种形式embebed一种形式,你应该使用formView的窗口小部件的选项指向不具有表单标签视图,如描述xupload工作流程维基

Q2:你应该使用maxNumberOfFiles在widget配置选项

这一切都应该是这样的:

 <?php
            $this->widget( 'xupload.XUpload', array(
                'url' => Yii::app( )->createUrl( "/fadepreciation/upload"),
                //our XUploadForm
                'model' => $photos,
                //We set this for the widget to be able to target our own form
                'htmlOptions' => array('id'=>'fa-depreciation-form'),
                'attribute' => 'file',
                'multiple' => true,
                //Note that we are using a custom view for our widget
                //Thats becase the default widget includes the 'form' 
                //which we don't want here
                'formView' => 'application.views.faDepreciation._form',
                'options' => array('maxNumberOfFiles' => 5)
                )    
            );
            ?>


Answer 4:

只要使用“showForm”参数如下:

<?php
$this->widget( 'xupload.XUpload', array(
  ...
  'showForm' => false,
  ...
));
?>

也许,在xupload的下一个版本中添加了该选项。



Answer 5:

我知道这是一个老的文章,但也许​​这个答案将帮助别人来解决这个问题。

我发现它是由在文件/xupload/views/form.php(使用默认设置)的最后一行造成的。 它看起来像if语句以某种方式相反的工作...采矿,对于虚假的价值它的渲染代码。 例如:

<?php 
echo $this->showForm; 
if($this->showForm) echo CHtml::endForm(); 
echo $this->showForm; 
?>

收益:

也许我失去了一些东西,但它看起来有点怪不是吗?



文章来源: yii ajax xupload form submit is not working