Yii2下载文件功能 [英] Yii2 Download File function
本文介绍了Yii2下载文件功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从/uploads文件夹下载文件,但是调用操作时,我只会收到timeOut错误, 任何人都可以帮助我:(
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 minutes
- 您可以使用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);
}
if (file_exists($file)) {
Yii::$app->response->xSendFile($file);
}
这篇关于Yii2下载文件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文