我正在尝试在 Controller 中下载文件,但在控制台中我只看到一堆奇怪的字符而不是下载框。我正在关注这个话题Symfony2 - Force file download .
不知道发生了什么...试图找到最简单的解决方案。 这是我的代码:
$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();
我什至尝试使用带有 header() 和 readfile() 的最基本示例。 我的服务器需要特殊配置吗?干杯。
最佳答案
您可以使用 Symfony 的内置BinaryFileResponse
,而不是重建这种响应。
use Symfony\Component\HttpFoundation\BinaryFileResponse;
$response = new BinaryFileResponse($file);
关于php - symfony - 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39603052/