kartik-v/yii2-widget-fileinput - how to remove the image?

Hello.

I use Kartik-v/yii2-widget-fileinput

How to handle the removal image?

_form.php:




echo $form->field($fileModel, 'file[]')->widget(FileInput::classname(), [

            'options' => [

                'multiple' => true

            ],

            'pluginOptions' => [

                'uploadUrl' => Url::to(['/news/upload-file']),

                'uploadExtraData' => [

                    'file_handler' => $uploadId

                ],

                

                'showPreview' => true,

                'showRemove' => true,

                'showUpload' => true,

                

                'browseClass' => 'btn btn-success',

                'uploadClass' => 'btn btn-info',

                'removeClass' => 'btn btn-danger',

                'removeIcon' => '<i class="glyphicon glyphicon-trash"></i>',

                'browseLabel' => 'Select files'

            ],

        ]);



By following line of code I handle upload:

‘uploadUrl’ => Url::to([’/news/upload-file’]),

How I can handle the delete action? So, when I press the recycle button, I need to send request to my server, to delete uploaded file.

You need initialPreviewConfig defined:


'initialPreviewConfig'=>[

'catpion'=>'...'

'url'=>'path/to/delete',

'key'=>'index for file will deleted',

'extra'=>['id'=>'set one indetify for element (optional)']

]

my option:

model:


public function initialPreviewConfig($urldel = ['/controller/action-delete-files'])

    {


      $return_json = [];

      foreach ($this->initialPreviewConfig as $k => $url) {


        $parts=pathinfo($url);

        $name  =  $parts['basename'];

        $return_json[] = [

          'caption'=>$name,

          'width'=> '100px',

          // 'url'=> \yii\helpers\Url::to($urldel),

          'key'=>$k,

          'extra'=>['id'=>$k]

        ];


      }


      return $return_json;

    }

My view


.....

'pluginOptions' => [ 

   ....

 'initialPreviewConfig'=>$model->initialPreviewConfig(),

'showRemove' => true,

]

my controller:


public function DeleteFiles(){

 .....

$model = new myModel;


  $file_key = (int)\Yii::$app->request->post('key');

      // your method del

      $delete = $model->deleteFiles($id_img);

}