K
K
kneht902020-05-17 09:26:48
Yii
kneht90, 2020-05-17 09:26:48

How to create a button that will download a file when clicked?

There is a small blog on Yii, I decided to make a button for downloading files. There is a public function in the Article.php file:

public function getFile()
    {
        $file = \Yii::getAlias('@app/files/' . $this->file);

        if (file_exists($file)) {
           \Yii::$app->response->sendFile($file);
        }
    }


I tried to call it in the article file like this, but when you open the article, the download of the file immediately starts without opening the contents of the article. There are similar questions, but for some reason through $_POST, GET does not work at all.

<a href="<?= $article->getFile();?>" download></a>


Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Barmunk, 2020-05-17
@Barmunk

Make the route site.ru/get-file/filename.jpg
filename.jpg to the file download function as an input parameter of this function.
And further

<a href="https://site.ru/get-file/filename.jpg" download></a>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question