R
R
Ruslan Tilyaev2019-11-06 18:08:16
Yii
Ruslan Tilyaev, 2019-11-06 18:08:16

How to link to a file?

Hello, how to specify a link to download a file (file.doc) on the site?
site on yii2, UrlManager included.
I did like this:

<a class="btn" href="<?=\yii\helpers\Url::to('@web/files/file.doc');?>"><i class="fa fa-arrow-down" aria-hidden="true"></i> Скачать резюме</a>

The result is a 404 error!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-11-06
@Heckfy325

Good evening.
The path to the file was not quite correctly indicated, and it is not necessary to do so.
You need to make a link to the action of the controller, and from the action to give the file.
Approximately so In the necessary controller you create action, transfer in it id of a file if the file data of a file is stored in a DB.

public function actionDownload($id)
{
  return Yii::$app->response->sendFile($filePath, "$file->name.$file->type", ['inline' => true]);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question