微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – symfony:setHttpHeader()不起作用,header()没有

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

这是代码

$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;

这在我的本地开发环境中工作正常 – 我的浏览器按预期获得标题,显示下载对话.

现在我更新了我的测试环境,用PHP 5.3.5-0.dotdeb.0运行Apache 2.2.9-10 lenny9.
如果我现在为测试环境调用该URL,我的浏览器不会获得任何自定义设置标头:

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

如果我在动作中通过header()手动设置它们,Firebug会按预期显示标题.
有人知道什么可能是错的吗?它是symfony bug,还是PHP或apache2配置问题?我不明白. : – /

提前致谢!

你的问题在这里
return sfView::NONE;

将其更改为:

return sfView::HEADERS_ONLY;

由于额外的评论编辑更新.

由于您尝试下载pdf,因此您无法正确解决问题.不要使用sendContent().请参阅下文(这是我编写的生产网站的片段,已经证明可以在所有主流浏览器中使用):

$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));

原文地址:https://www.jb51.cc/php/130607.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐