symfony - 下载文件

bet*_*son 0 php header download symfony

我正在尝试在控制器中下载文件,但在控制台中我只看到一堆奇怪的字符而不是下载框。我正在关注这个主题Symfony2-Force 文件下载

不知道发生了什么......试图找到最简单的解决方案。这是我的代码:

 $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();
Run Code Online (Sandbox Code Playgroud)

我什至尝试将最基本的示例与 header() 和 readfile() 一起使用。我的服务器需要特殊配置吗?干杯。

Mic*_*ler 5

您可以使用 Symfony 的内置BinaryFileResponse.

use Symfony\Component\HttpFoundation\BinaryFileResponse;

$response = new BinaryFileResponse($file);
Run Code Online (Sandbox Code Playgroud)

另请查看有关服务文件文档