2016-09-20 2 views
0

컨트롤러에서 파일을 다운로드하려고하는데 콘솔에서 다운로드 상자 대신 별난 문자 만 표시됩니다. 나는이 주제를 따르고있다 Symfony2 - Force file download.symfony - 다운로드 파일

무슨 일이 일어나고 있는지 모릅니다 ... 가장 간단한 해결책을 찾고 있습니다. 여기 내 코드입니다 :

$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도 참조하십시오.