Symfony2:如何强制下载

时间:2012-05-11 14:07:35

标签: symfony download

我正在尝试使用symfony2创建一个下载文件的链接。它确实下载了一个文件,但它没有用,因为它是零octect。我不知道如何让它发挥作用。有人知道该怎么做吗?

该文件位于web/uploads/documents/

以下是控制器中的代码:

// here I get the name of the file

  $response = new Response();
  $response->headers->set('Content-type', 'application/octet-stream');
  $response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $filename));

  return $response;

我尝试编写整个路径而不是$filename,但它会更改下划线中的所有斜杠。

1 个答案:

答案 0 :(得分:4)

您忘了设置内容:

$response->setContent(file_get_contents($localFilePath));
相关问题