2016-11-29 72 views
0

大家好!我正在使用Laravel 5.2应用程序,用户可以在其中下载各种文件。其中之一是“用户指南”,解释如何设置网站和功能等。我希望PDF能够在另一个页面中流动,以便用户仍在应用程序中。我现在用的控制器是:在Laravel 5.2中流式传输PDF文件

public function userguidePDF(){ 

    return response()->stream('../public/download/userguide.pdf'); 

} 

但这返回:

参数1传递给 的Symfony \分量\ HttpFoundation \ StreamedResponse :: __结构()必须 可调用,字符串中给定,称为在 /path/to/laravel/vendor/laravel/framework/src/Illuminate/Routing/ResponseFactory.php 上线117和限定

我已经在互联网上搜索的方法,其含铅我的语法如下:

return response()->stream($callback, 200, $headers); 

不幸的是,我无法找到有关参数的详细资料,因为我不理解他们。有人请向我解释$callback, 200, $header是什么参数以及我如何使用它?

回答

1

$回调应该是一个输出您的PDF的函数。例如:

 $callback = function() 
     { 
       $handle = fopen("../public/download/userguide.pdf", "r"); 
       $filecontent= fread($handle, filesize("../public/download/userguide.pdf")); 
       fclose($handle); 

       return $filecontent; 
     }; 
+0

什么用标题是'200'和'$ header'? –

+0

什么是变量'$ filename'? –

+0

200只是HTTP状态码(请参阅https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success)。 – Sitethief

0

您可以使用下载也

return Response::download($file, 'filename.pdf', $headers); 

这里是流媒体文件,并立即下载。 https://laravel.com/api/5.2/Illuminate/Routing/ResponseFactory.html#method_stream

编辑

这样

// We'll be outputting a PDF 
header('Content-Type: application/pdf'); 

// It will be called downloaded.pdf 
header('Content-Disposition: attachment; filename="downloaded.pdf"'); 
+0

嗨!我有下载工作,但我特别想要一个流方法。 –

+0

那么你可以使用@sitethief –

+0

给出的回调示例那个也不行。 –