Yii2 下载文件功能 [英] Yii2 Download File function
本文介绍了Yii2 下载文件功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从文件夹/uploads 下载文件,但在调用操作时出现超时错误,任何人都可以帮助我:(
I need to download file from folder /uploads, but I just get timeOut error when the action is called, Anyone can help me :(
public function actionDownload() {
$path = Yii::getAlias('@webroot') . '/uploads';
$file = $path . '/1.pdf';
if (file_exists($file)) {
Yii::$app->response->sendFile($file);
}
}
推荐答案
如果下载时间过长,我看到了 2 种可能性
If a download takes too much time, I see 2 possibilities
- 您可以增加脚本的最大执行时间.这不是最好的解决方案,因为脚本对于太大的文件仍然会超时,但这是最简单的解决方案(可能存在与您的问题无关的性能考虑).这样做:
ini_set('max_execution_time', 5*60);//5 分钟
- 您可以使用 Apache 的 X-SendFile 标头(如果此模块仅在 Apache 中启用)让 Apache 处理文件的发送.更多关于 Yii2 文档的信息 http://www.yiiframework.com/doc-2.0/guide-runtime-responses.html#sending-files.当心 IE 中的错误<=8.
- You can use the X-SendFile header of Apache (if this module in enabled in Apache only) to let Apache handle the sending of the file. More about this on Yii2 documentation http://www.yiiframework.com/doc-2.0/guide-runtime-responses.html#sending-files. Beware of bugs in IE<=8.
if (file_exists($file)) {Yii::$app->response->xSendFile($file);}
这篇关于Yii2 下载文件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文