douluogu8713 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,
        ));
 $response->send();

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

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大