Search code examples
ajaxyii2yii-extensionsyii2-advanced-app

Passing extra data to yii uploader


Am creating a file uploader using the kartik uploader bu the problem is that it doesn't pass extra data

The form(view) code

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

]
);?>

The $evidence ,path is a table related to the models table

The controller that I have tested with (code)

public function actionUpload(){

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

}

This returns null showing that the album_id is not passed to the controller and yet the $model->case_description is the field above the upload widget

The new controller

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

Solution

  • Due the problem with dinamic/variable assign to extraData i suggest a simple solution based on POST / submit method (eventually set the proper action in your form)

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