简体   繁体   English

使用php yii2在Chrome中Excel文件下载失败

[英]Excel file download failed in Chrome using php yii2

<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. 我已经检查了此问题,但未找到任何解决方案。

在此处输入图片说明

In above picture it shows that there is not file. 在上图中,它表明没有文件。

Any help would be highly appreciated. 任何帮助将不胜感激。

From google chrome help center Google Chrome帮助中心

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. Chrome会自动恢复下载。 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. 如果1和2不起作用。 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 . 您可以通过检查file_exists进行修复,并通过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 . 您可以在任何控制器中使用此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM