1

我正在尝试使用 CMultiFileUpload 和 CUploadedFile 实现多文件上传,但它不起作用。具体来说,即使考虑到我在视图中的选项中使用 'enctype' => 'multipart/form-data' , _POST 也不起作用:

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

)); ?>

这是用于 CMultiFileUpload 的小部件和参数:

        <div class="row">
            <?php echo $form->labelEx($model,'archivo_foto')?>
            <?php //echo CHtml::activeFileField($model,'archivo_foto')?>

            <?php $this->widget('CMultiFileUpload', array(
                    'model' => $model,
                    'name' => 'archivo_foto',
                    'accept' => 'jpeg|jpg|gif|png|txt', // useful for verifying files
                    'duplicate' => 'Duplicate file!', // useful, i think
                    'denied' => 'Invalid file type', // useful, i think
                    'max' => 10,
                    'htmlOptions' => array( 'multiple' => 'multiple', 'size' => 25 ),
            )); ?>

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

另一方面,控制器动作是这样实现的:

        public function actionUpdateam($id)
    {
            $model=$this->loadModel($id);
            $dir=Yii::getPathOfAlias('application.uploads');
            $model->archivo_documento='funciona 0';
            if(isset($_POST['Examen'])) {

                    $model->attributes=$_POST['Examen'];

                    // THIS is how you capture those uploaded images: remember that in your CMultiFile widget, you set 'name' => 'archivo_foto'
                    $images = CUploadedFile::getInstancesByName('archivo_foto');

                    // proceed if the images have been set
                    $model->archivo_documento='funciona uno';
                    if (isset($images) && count($images) > 0) {
                            $model->archivo_documento='funciona dos';
                            // go through each uploaded image
                            foreach ($images as $image) {
                                    echo $image->name.'<br />';
                                    $image->saveAs($dir.'/'.$image->name);
                                    $model->archivo_foto = $model->archivo_foto."+".$image->name;
                            }

                            // save the rest of your information from the form
                            if ($model->save()) {
                                    $this->redirect(array('view','id'=>$model->id));
                            }
                    }

            }

            $this->render('update_am',array(
                    'model'=>$model,
            ));
    }

最后,我认为提到用于模型的规则很重要(这也可能是问题的原因):

array('archivo_foto','file','allowEmpty'=>true,'maxFiles'=>10),

我认为问题出在 post 方法中,因为控制器没有上传文件,也没有在数据库中进行任何更改。但我不确定。

4

3 回答 3

0

尝试更改为'htmlOptions'=>array('multiple'=>true).

于 2014-02-10T15:40:54.323 回答
0

我正在使用相同的扩展名,这些代码对我来说很有效 在视图文件中

<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'adpost-form',
'enableClientValidation'=>true,
'clientOptions' => array(
     'validateOnSubmit'=>true,
     'validateOnChange'=>false,
     'afterValidate'=>'js:submiAjaxForm'
 ),
'htmlOptions' => array('enctype' => 'multipart/form-data'),

)); ?>

<?php
                $this->widget('CMultiFileUpload', array(
                    'name' => 'photo_name',
                    'accept' => 'jpeg|jpg|gif|png',
                    'duplicate' => 'File is not existed!',
                    'denied' => 'Not images', // useful,
                    'htmlOptions' => array(
                        'style' =>'color: transparent;',
                    ),
                ));
                ?>

在控制器中

$images = CUploadedFile::getInstancesByName('photo_name');
foreach ($images as $image => $pic) {

                        //----------------- Renaming image before uploading

                        $extension      =   $pic->getExtensionName();
                        $newName    =   "image_".$adModel->id;
                        $newName                .=  "_".$imgCount.".".$extension;
                        $imgCount++;

                        if ($pic->saveAs($newPath.$newName)) {
                            // add it to the main model now
                            $img_add             = new AdsPhotosTbl;
                            $img_add->photo_name = $newName;
                            $img_add->ad_id      = $adModel->id;   
                            $img_add->status     = 1;
                            if(!$img_add->save()){
                                $error           =   true;
                                $upload_error    =   true;
                                break;
                            } 
                        }else{
                            $upload_error       =   true;
                            $error              =   true;
                            break;
                        }
                    }
于 2014-10-10T10:18:10.750 回答
0

在表单中添加以下代码

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

喜欢

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'form',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>true,

     'htmlOptions' => array(
        'enctype' => 'multipart/form-data',
    ),
)); ?>
于 2014-02-18T13:29:56.193 回答