加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 14:13:35 所属栏目:PHP教程 来源:网络整理
导读:我在symfony中构建了一个简单的动作,它通过wkhtmltopdf生成一个PDF文件并将其输出到浏览器. 这是代码: $response = $this-getResponse(); $response-setContentType('application/pdf'); $response-setHttpHeader('Content-Disposition',"attachment; filen
我在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));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读