Yii2 sendfile response

tutulang picture tutulang · May 13, 2015 · Viewed 9.2k times · Source

I am new to Yii2 and I would really appreciated if anyone could help me to solve the problem. I want to make a download link and I use

Yii::$app->response->sendFile($path, $name, ['inline' => false])->send();

in download function but it just read the content of the file and display straight in the page. What I want to do is when users click the download link, the download dialog will pop up or the file will be automatically downloaded, rather than display the content. Can anyone please help me?

Answer

Marcin picture Marcin · Sep 30, 2015

I had exactly the same problem. It turned out that my link to download action was placed inside the Pjax::begin() Pjax::end() methods, so the content of the file was treated as update for the page.

My solution: add data-pjax="0" attribute to download link.