Yii2 下载文件功能 [英] Yii2 Download File function

查看:36
本文介绍了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 分钟

  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆