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

Passing extra data to yii uploader

我正在使用 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 是与模型 table

相关的 table

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

public function actionUpload(){

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

}

这个 returns null 表示 album_id 没有传递给控制器​​,但是 $model->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);

由于 dinamic/variable 分配给 extraData 的问题,我建议一个基于 POST / 提交方法的简单解决方案(最终在您的表单中设置正确的操作)

    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();
?>