我对生成的文件有问题。这是我的行动:
<?php
public function exportAction()
{
$this->checkSecurity('EDIT');
$config = $this->getConfiguration();
$resource = $this->findOr404();
$style = $this->render($config->getTemplate('export.css'), array(
'resource' => $resource
));
$response = new Response();
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', 'plain/text');
$response->headers->set('Content-type', 'application/octet-stream');
$response->headers->set('Content-Disposition', 'attachment; filename="style-' . $resource->getSlug() . '.css";');
$response->sendHeaders();
$response->setContent($style);
return $response;
}
我的文件已下载,但其内容的开头是:
HTTP/1.0 200 OK
Cache-Control: no-cache
Date: Tue, 02 Sep 2014 09:16:34 GMT
有谁知道为什么?
答案 0 :(得分:0)
我不会使用响应作为返回生成的文件进行下载。
我只需填写一个变量,然后显示如下:
$style = $this->render($config->getTemplate('export.css'), array(
'resource' => $resource
));
header("Content-Type: text/calendar; filename='style-" . $resource->getSlug() . ".css'");
header('Content-Length: '.strlen($style);
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: inline; filename='style-" . $resource->getSlug() . ".css'");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
echo $style;
exit;
我知道这不是绝对的对象端代码,但它非常适合在我们的项目中每天生成数千个ics文件
答案 1 :(得分:0)
我终于找到了解决方案。我忘记了渲染
上的getContent()<?php
public function exportAction()
{
$this->checkSecurity('EDIT');
$config = $this->getConfiguration();
$resource = $this->findOr404();
$style = $this->render($config->getTemplate('export.css'), array(
'resource' => $resource
));
$response = new Response();
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', 'plain/text');
$response->headers->set('Content-type', 'application/octet-stream');
$response->headers->set('Content-Disposition', 'attachment; filename="style-' . $resource->getSlug() . '.css";');
$response->sendHeaders();
$response->setContent($style->getContent());
return $response;
}