使用 php yii2 在 Chrome 中下载 Excel 文件失败
<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.
上图显示没有文件.
任何帮助将不胜感激.
推荐答案
来自 谷歌浏览器帮助中心
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:
确保您的互联网连接正常工作.如果您的互联网连接不稳定,请了解如何解决互联网稳定性问题.
Make sure your internet connection is working normally. If your internet connection is unstable, learn how to fix internet stability issues.
.Chrome 会自动恢复下载.您也可以点击向下箭头向下箭头,然后点击继续"或更多",然后点击继续"来继续下载文件.
. 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.
尝试稍后下载文件.
联系网站所有者.
如果 1 和 2 不起作用.如果您尝试使用其他浏览器,那么问题出在您的文件和服务器上.
If 1 and 2 not work. and you trying with other browsers then the problem is in your file and server.
您可以通过检查 file_exists 来修复它并通过以下方式发送文件Yii2 sendFile.见例子:
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 .
相关文章