BinaryFileResponse和APP_ENV = prod

时间:2018-09-27 08:04:29

标签: symfony symfony4

我在使用Symfony\Component\HttpFoundation\BinaryFileResponse的Symfony 4中遇到文件下载问题。

APP_ENV=dev切换到APP_ENV=prod(例如在本地且已清除缓存)时,浏览器仅接收响应标头(注意内容长度):

Date: Thu, 27 Sep 2018 07:47:17 GMT
Server: Apache/2.4.18 (Ubuntu)
Cache-Control: max-age=0, must-revalidate, private
Last-Modified: Thu, 27 Sep 2018 07:47:22 GMT
Content-Disposition: inline; filename=kJKpFb
Content-Length: 0
Accept-Ranges: bytes
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/pdf

用于执行下载的代码:

$response = new BinaryFileResponse($file->getPathname());
$response->headers->set('Content-Type', 'application/pdf');
$response->setContentDisposition(
    ResponseHeaderBag::DISPOSITION_INLINE,
    $file->getFilename()
);

return $response;

1 个答案:

答案 0 :(得分:0)

Symfony v3.2引入了File controller helper。在4.x example 1 example 2

中应使用相同的语法