Laravel:使用Response :: download缓慢下载

时间:2014-10-31 09:08:59

标签: php laravel laravel-4

所以我有一个大的(1.7mb)jpg文件,我想让用户下载为屏幕壁纸。我使用以下内容:

/**
* getWallpaper
* Download the wallpaper in jpg format from above web root
* 
* @return file
*/
public function getWallpaper()
{
$file = "../downloads/myfile.jpg";

$headers = array('Content-Type: image/jpeg');

return Response::download($file, 'myfile.jpg', $headers);
}

这在我的本地开发服务器上运行得非常快,但浏览器下载对话框大约需要14秒才能显示在我的生产服务器上。 有任何想法吗?可能是服务器级别的设置未匹配,如果是,我应该查看哪些设置? 谢谢你的帮助。 奥利。

2 个答案:

答案 0 :(得分:0)

我只是在这里猜测,但您可以尝试设置附件和内容类型标题,然后立即调用flush()将该信息发送给客户端,以便打开对话框。

public function getWallpaper(){
    $file = "../downloads/myfile.jpg";

    header('Content-Type: image/jpeg');
    header('Content-Disposition: attachment; filename="myfile.jpg"');

    flush();

    return Response::download($file, 'myfile.jpg');
}

在我看来,它感觉有点hacky(多余,因为我们设置的标题然后再次在Response::download中设置,但如果它有效,你可以扩展Response类什么的。

答案 1 :(得分:0)

以防任何人遇到同样的问题 - 对我而言,结果证明是网络连接问题。

在我的工作连接上,我仍然会在下载对话框出现之前得到很长时间的延迟,而在家中它几乎立即出现。我不知道为什么!