2016-09-20 62 views
0

我試圖在控制器中下載文件,但在控制檯中,我只看到了一堆真正的奇怪字符,而不是下載框。我正在關注這個主題Symfony2 - Force file downloadsymfony - 下載文件

不知道發生了什麼...試圖找到最簡單的解決方案。 這裏是我的代碼:

$response = new Response(file_get_contents($file->realPath), 200, array(
      'Content-Type' => $file->mimeType, 
      'Content-Length' => filesize($file->realPath), 
      'Content-Disposition' => 'attachment; filename=' . $file->name, 
     )); 
$response->send(); 

我甚至試圖用與報頭中的最基本的例子()和ReadFile的()。 我的服務器需要特殊配置嗎?乾杯。

回答

3

您可以使用Symfony的內置BinaryFileResponse來代替重建這種響應。

use Symfony\Component\HttpFoundation\BinaryFileResponse; 

$response = new BinaryFileResponse($file); 

請看一看documentation about serving files