2016-11-29 31 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

給出的回調示例那個也不行。 –