0

我已经尝试了很多方法来使用 CMultiFileUpload 小部件使多个文件上传可行。我已经检查并关注了以下链接-

http://www.yiiframework.com/forum/index.php/topic/47665-multiple-file-upload/

Yii 多文件上传

但仍然没有运气!

这是我的代码:

看法:

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

<?php
$this->widget('CMultiFileUpload', array(
'model'=>$model,
'name'=>'image_filename',
'attribute'=>'image_filename',
'accept'=>'jpg|gif|png',
'options'=>array(
    // 'onFileSelect'=>'function(e, v, m){ alert("onFileSelect - "+v) }',
    // 'afterFileSelect'=>'function(e, v, m){ alert("afterFileSelect - "+v) }',
    // 'onFileAppend'=>'function(e, v, m){ alert("onFileAppend - "+v) }',
    // 'afterFileAppend'=>'function(e, v, m){ alert("afterFileAppend - "+v) }',
    // 'onFileRemove'=>'function(e, v, m){ alert("onFileRemove - "+v) }',
    // 'afterFileRemove'=>'function(e, v, m){ alert("afterFileRemove - "+v) }',
),
'denied'=>'File is not allowed',
'max'=>10,
'htmlOptions' => array( 'multiple' => 'multiple'),
));
?>

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

控制器:

当我使用 GET 方法查看表单提交功能时,我发现提交后显示的文件名。但是,使用 POST 方法我没有看到里面的名字if(isset($_POST['Images']))。即使我没有看到里面的回声结果。

public function actionPhoto()
{            
  $model=new Images;
  if(isset($_POST['Images']))
  {
    echo 1;//Test purpose. Not displaying after form submission!!

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

    $image_filename = CUploadedFile::getInstancesByName('image_filename');

    // proceed if the images have been set
    if (isset($image_filename) && count($image_filename) > 0)
    {
        // go through each uploaded image
        foreach ($image_filename as $image_filename => $pic)
        {
            echo $pic->name.'<br />';//Check purpose

            if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/images/'.$pic->name))
            {  
                $Images = new Images();
                $Images->image_filename = $pic->name;

                $Images->save();
            }
        }
    }
}

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

模型规则:

public function rules()
{
  return array(
    array('image_filename', 'file', 'types'=>'jpg, jpeg, png, gif','allowEmpty'=>false, 'on'=>'insert'),       
    array('id, image_filename', 'safe'),
    array('id, image_filename', 'safe', 'on'=>'search'),
);
}

我的代码有什么问题?

感谢您的帮助和友好合作。

4

1 回答 1

0

在您的代码中,更改以下行

$image_filename = CUploadedFile::getInstancesByName('$image_filename');

$image_filename = CUploadedFile::getInstancesByName('image_filename');
于 2014-02-05T06:40:32.197 回答