2016-09-20 20:16
浏览 57

symfony - 下载文件

I am trying download files in controller but in the console I see only really bunch of weird characters instead of download box. I am following this topic Symfony2 - Force file download.

Don't know what is going on... trying to find simplest solution. Here is my code:

 $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,

I've even tried to use the most basic example with header() and readfile(). Does my server need special config or something? Cheers.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在控制器中下载文件但在控制台中我只看到一堆奇怪的字符而不是下载框。 我正在关注此主题 Symfony2 - 强制文件下载 < p>不知道发生了什么......试图找到最简单的解决方案。 这是我的代码:

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

我甚至尝试使用header()的最基本的例子, readfile()。 我的服务器需要特殊配置吗? 干杯。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtuo5611 2016-09-21 07:22

    Instead of rebuilding that kind of response, you could use Symfony's built-inBinaryFileResponse.

    use Symfony\Component\HttpFoundation\BinaryFileResponse;
    $response = new BinaryFileResponse($file);

    Please take also a look in the documentation about serving files.

    解决 无用
    打赏 举报

相关推荐 更多相似问题