1

Q1:表单提交不起作用。

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

status : 创建一个带有 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
4

5 回答 5

1

您需要做的是创建一个自定义表单。从 xupload _form 复制内容并粘贴它,删除开始表单 - 结束表单。将自定义表单中的引用添加到您的小部件“formView”。

于 2012-08-27T15:05:37.190 回答
0

只需使用 'showForm' 参数,如下所示:

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

也许,这个选项被添加到下一个版本的 xupload 中。

于 2013-05-27T17:59:40.717 回答
0

Q1:表单提交不起作用,因为 XUpload 小部件生成了自己的表单标签。所以您生成的 HTML 有一个嵌入在另一个表单中的表单,您应该使用formView小部件的选项来指向一个没有表单标签的视图,如xupload 工作流 wiki中所述

Q2:您应该maxNumberOfFiles在小部件配置中使用选项

这一切都应该是这样的:

 <?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)
                )    
            );
            ?>
于 2012-09-27T14:31:35.797 回答
0

提交表格有什么问题?

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

于 2012-08-02T08:51:33.980 回答
0

我知道这是一篇旧帖子,但也许这个答案会帮助某人解决这个问题。

我发现这是由文件 /xupload/views/form.php 中的最后一行引起的(使用默认设置)。看起来 if 语句在某种程度上是相反的……在挖掘错误值时,它正在渲染代码。例如:

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

返回: 奇怪的输出

也许我错过了一些东西,但它看起来很奇怪......不是吗?

于 2014-04-14T20:25:31.427 回答