我在使用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;