Vitaly Park
Vitaly Park

Reputation: 57

how to display uploaded file in yii2 views and interact with it?

I added file uploading function to my controller, file is stored in the specified directory, but views like update, index, view shows only the name of the file. I need a link or a button to interact with this uploaded file. For example opening file by pressing on this link or button also downloading this file. Could you help me with it?

Model:

public function rules()
{
    return [
    ...
        [['attachment'],'file'],
    ];
}

Controller:

public function actionCreate()
{
    $model = new Letter();

    if ($model->load(Yii::$app->request->post())) {
        $model->attachment = UploadedFile::getInstance($model, 'attachment');

        $filename = pathinfo($model->attachment , PATHINFO_FILENAME);
        $ext = pathinfo($model->attachment , PATHINFO_EXTENSION);

        $newFname = $filename.'.'.$ext;

        $path=Yii::getAlias('@webroot').'/uploads/';
        if(!empty($newFname)){
            $model->attachment->saveAs($path.$newFname);
            $model->attachment = $newFname;
            if($model->save()){
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }
    }
    return $this->render('create', [
        'model' => $model,
    ]);

Form View:

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

<?= $form->field($model, 'attachment')->fileInput() ?>

<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

Thanks in advance.

Upvotes: 1

Views: 4724

Answers (1)

jithin
jithin

Reputation: 920

update your form view like below:

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

<?= $form->field($model, 'attachment')->fileInput() ?>

 /*link to download file*/
<?if(!$model->isNewRecord):?>
<?= Html::a('Download file', ['download', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?endif;?>

<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>


<?php ActiveForm::end(); ?>

in your controller add new action for download file:

public function actionDownload($id) 
{ 
    $download = Letter::findOne($id); 
    $path=Yii::getAlias('@webroot').'/uploads/'.$download->attachment;

    if (file_exists($path)) {
        return Yii::$app->response->sendFile($path);
    }
}

in your grid view :

 <?= GridView::widget([
'dataProvider' => $dataProvider,
'id'=>'mygrid',
'columns' => [
['class' => 'yii\grid\SerialColumn'],



[
'attribute'=>'attachment',
'format'=>'raw',
'value' => function($data)
{
    return
    Html::a('Download file', ['letter/download', 'id' => $data->id], ['class' => 'btn btn-primary']);

}
],

],
]); ?>

Upvotes: 1

Related Questions