Excel file download failed in Chrome using php yii

2019-03-01 08:37发布

<a href="/Files/file.xxxx" download class="btn btn-success">Download File</a>

I have a page in which I have a download button that should download the file. But it failed to download. I have checked this issue but didn't find any solution.

enter image description here

In above picture it shows that there is not file.

Any help would be highly appreciated.

1条回答
Deceive 欺骗
2楼-- · 2019-03-01 09:26

From google chrome help center

If you try to download a file and it doesn’t work, first try to fix the error with these troubleshooting steps:

  1. Make sure your internet connection is working normally. If your internet connection is unstable, learn how to fix internet stability issues.

    . Chrome will automatically resume the download. You can also resume the file download by clicking the Down arrow Down Arrow and then Resume or More More and then Resume.

  2. Try to download the file later.

  3. Contact the website owner.

If 1 and 2 not work. and you trying with other browsers then the problem is in your file and server.

You can fix it by checking file_exists and send file by Yii2 sendFile. see example:

$file_path = '/route/to/path/file.xlsx';
if (file_exists($file_path)) {
    return Yii::$app->response->sendFile($file_path);
}else{
    return 'file not found';
}

you can use this code in any controller .

查看更多
登录 后发表回答