symfony的:setHttpHeader()不工作,头()做 [英] symfony: setHttpHeader() doesn't work, header() does

查看:209
本文介绍了symfony的:setHttpHeader()不工作,头()做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建在symfony中一个简单的动作它通过wkhtmltopdf生成的PDF文件,并​​将其输出到浏览器中。

I built a simple action in symfony which generates a PDF-file via wkhtmltopdf and outputs it to the browser.

这里的code:

  $response = $this->getResponse();
  $response->setContentType('application/pdf');
  $response->setHttpHeader('Content-Disposition', "attachment; filename=filename.pdf");
  $response->setHttpHeader('Content-Length', filesize($file));
  $response->sendHttpHeaders();
  $response->setContent(file_get_contents($file));
  return sfView::NONE;

这是在我的本地开发环境工作得很好 - 我的浏览器获得头不如预期,显示下载对话

That works fine in my local development environment - my browser gets the headers as expected, showing the download-dialogue.

现在我更新了我的测试环境,运行Apache 2.2.9-10 + lenny9用PHP 5.3.5-0.dotdeb.0。
如果我现在请该URL的测试环境,我的浏览器没有得到任何自定义设置头:

Now I updated my testing-environment, running Apache 2.2.9-10+lenny9 with PHP 5.3.5-0.dotdeb.0. If i call that URL now for the testing-environment, my browser don't get any custom set headers:

Date    Mon, 07 Mar 2011 10:34:37 GMT
Server  Apache
Keep-Alive  timeout=15, max=100
Connection  Keep-Alive
Transfer-Encoding   chunked

如果我通过我的动作头()手动设置,萤火显示标题预期。
有谁知道什么地方出错了?它是一个symfony的bug,或者一个PHP或Apache2的配置问题?我不明白这一点。 : - /

If I set them manually via header() in my action, Firebug shows the headers as expected. Does anybody know what could be wrong? Is it a symfony bug, or a php or apache2 configuration issue? I don't get it. :-/

在此先感谢!

推荐答案

您的问题就在这里:

return sfView::NONE;

更改为:

return sfView::HEADERS_ONLY;

修改更新,由于额外的评论。

edit update due to extra comments.

既然你想下载一个PDF,你不正确处理这个问题。不要使用 sendContent()。见下面(这是从生产现场我写了一个片段,并已被证明在所有主要的浏览器工作):

Since you are trying to download a pdf, you're approach the problem incorrectly. Do not use sendContent(). See below (this is a snippet from a production site I've written and has proven to work across all major browsers):

$file = '/path/to/file.pdf';
$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setStatusCode(200);
$this->getResponse()->setContentType('application/pdf');
$this->getResponse()->setHttpHeader('Pragma', 'public'); //optional cache header
$this->getResponse()->setHttpHeader('Expires', 0); //optional cache header
$this->getResponse()->setHttpHeader('Content-Disposition', "attachment; filename=myfile.pdf");
$this->getResponse()->setHttpHeader('Content-Transfer-Encoding', 'binary');
$this->getResponse()->setHttpHeader('Content-Length', filesize($file));

return $this->renderText(file_get_contents($file));

这篇关于symfony的:setHttpHeader()不工作,头()做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆