将额外数据传递给yii上传器

时间:2015-11-20 18:25:52

标签: ajax yii2 yii-extensions yii2-advanced-app

使用kartik上传器创建文件上传器问题是它没有传递额外数据

表单(视图)代码

 <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

<?= $form->field($model, 'case_description')->textArea();?>


    <?php   echo $form->field($evidence, 'path')->widget(FileInput::classname(), [
                    'options' => ['accept' => '*','multiple' => true,'name'=>'images'],
                    'pluginOptions' => [
                 'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ',
                    'showPreview' => true,
                    'showCaption' => true,
                    'showRemove' => true,
                    'showUpload' => true,

                    'browseLabel' =>  'Insert Evidence',
                   'uploadUrl' => Url::to(['cases/upload']),
                   'maxFileCount' => 10,
                'uploadExtraData' => [
                      'album_id' => "javascript: return $('#cases-case_description').val());",
                  ],
                ],              

]
);?>

$ evidence,path是一个与模型表相关的表

我测试过的控制器(代码)

public function actionUpload(){

$ann = empty($_POST['album_id']) ? '' : $_POST['album_id'];
var_dump($ann)

}

这将返回null,表示album_id未传递给控制器​​,而$ model-&gt; case_description是上传小部件上方的字段

新控制器

public function actionUpload(){

$images = $_FILES['evidence'];
$success = null;

$paths= ['uploads'];

// get file names
$filenames = $images['name'];


// loop and process files
for($i=0; $i < count($filenames); $i++){
    //$ext = explode('.', basename($filenames[$i]));
    $target = "uploads/cases/evidence".DIRECTORY_SEPARATOR . md5(uniqid()); //. "." . array_pop($ext);
    if(move_uploaded_file($images['name'], $target)) {
        $success = true;
        $paths[] = $target;
    } else {
        $success = false;

        break;
    }

    echo $success;
}
    // check and process based on successful status 
if ($success === true) {


    $output = [];
} elseif ($success === false) {
    $output = ['error'=>'Error while uploading images. Contact the system administrator'];

    foreach ($paths as $file) {
        unlink($file);
    }
} else {
    $output = ['error'=>'No files were processed.'];
}

// return a json encoded response for plugin to process successfully
echo json_encode($output);

1 个答案:

答案 0 :(得分:1)

由于dinamic / variable赋值给extraData的问题我建议一个基于POST / submit方法的简单解决方案(最终在你的表单中设置正确的动作)

    use kartik\widgets\FileInput

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

    <?= $form->field($model, 'case_description')->textArea();?>


        <?php   echo $form->field($evidence, 'path')->widget(FileInput::classname(), [
                        'options' => ['accept' => '*','multiple' => true,'name'=>'images'],
                        'pluginOptions' => [
                        'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ',
                        'showPreview' => true,
                        'showCaption' => true,
                        'showRemove' => true,
                        'showUpload' => true,

                        'browseLabel' =>  'Insert Evidence',
                        'uploadUrl' => Url::to(['cases/upload']),
                        'maxFileCount' => 10,
                    ],              

                ]
        );

        echo Html::submitButton($model->isNewRecord ? 'Upload' : 'Update', [
            'class'=>$model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']
            );
        ActiveForm::end();
?>