php Zend Framework 如何设置标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1326270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Zend Framework how to set headers
提问by Uffo
I have a question, how can I do something like this:
我有一个问题,我该如何做这样的事情:
header("Content-Disposition: inline; filename=result.pdf");
header("Content-type: application/x-pdf");
With Zend Framework, I have tried:
使用 Zend 框架,我尝试过:
$this->getResponse()
->setHeader('Content-Disposition:inline', ' filename=result.pdf')
->setHeader('Content-type', 'application/x-pdf');
But doesn't work correctly.
但不能正常工作。
Best Regards,
此致,
回答by Stefan Gehrig
Your statement to set the response headers is slightly malformed:
您设置响应标头的语句格式略有错误:
$this->getResponse()
->setHeader('Content-Disposition', 'inline; filename=result.pdf')
->setHeader('Content-type', 'application/x-pdf');
The above should work - please note the difference in the Content-Disposition-header.
以上应该有效 - 请注意Content-Disposition-header 中的差异。
By the way... When you want to force a download box (instead of loading the document in the browser) you should use the Content-Dispositionattachment.
顺便说一句...当您想强制下载框(而不是在浏览器中加载文档)时,您应该使用Content-Dispositionattachment.
$this->getResponse()
->setHeader('Content-Disposition', 'attachment; filename=result.pdf')
->setHeader('Content-type', 'application/x-pdf');
Depending on the browser it may be possible that you also have to set the Content-Lengthor change the Content-typeto a combination (multiple headers) of one or more of application/force-download, application/octet-streamand/or application/download. And as I wrote in the comment sometimes caching headers may interfere with your download. Check to see which caching-headers are sent.
根据浏览器的不同,您可能还必须将Content-Length或 更改Content-type为application/force-download,application/octet-stream和/或 中的一个或多个的组合(多个标题)application/download。正如我在评论中所写,有时缓存标头可能会干扰您的下载。检查以查看发送了哪些缓存头。
回答by David Snabel-Caunt
Late to the table, I can recommend this action helperas a simple, reusable component for sending files or in memory data to the browser.
迟到了,我可以推荐这个动作助手作为一个简单的、可重用的组件,用于将文件或内存中的数据发送到浏览器。
Has options for caching, disposition and can utilise Apache Sendfile
有缓存、处置选项,可以利用 Apache Sendfile
回答by Justin
My guess is that you're doing something like:
我的猜测是你正在做这样的事情:
$this->getResponse()
->setHeader('Content-Disposition:inline', ' filename=result.pdf')
->setHeader('Content-type', 'application/x-pdf');
fpassthru($filename);
exit();
or something.
或者其他的东西。
The response here will never be rendered (which renders the headers). The response is rendered during post-action printing, usually.
永远不会呈现此处的响应(呈现标题)。通常在动作后打印期间呈现响应。
You will have to directly set the headers (as you noted in the non-oo code), or use $this->getResponse()->sendHeaders()directly.
您必须直接设置标头(如您在非 oo 代码中所述),或$this->getResponse()->sendHeaders()直接使用。
回答by Guntram
I had a header set. It was not set, but ADDED. So I had a Content-Type of text/html and also application/pdf.
我有一个标题集。它没有设置,而是添加了。所以我有一个 text/html 的 Content-Type 和 application/pdf。
Flagging the Content-Type with TRUE made the download possible in IOS and other devices which showed only cryptic symbols after the download or an error:
使用 TRUE 标记 Content-Type 使得在 IOS 和其他设备中下载成为可能,这些设备在下载或错误后仅显示神秘符号:
->setHeader('Content-type', 'application/x-pdf', true);
->setHeader('Content-type', 'application/x-pdf', true);
setHeader($name, $value, $replace = false)
setHeader($name, $value, $replace = false)
from: https://framework.zend.com/manual/1.12/de/zend.controller.response.html
来自:https: //framework.zend.com/manual/1.12/de/zend.controller.response.html
回答by Uffo
Solved
解决了
$this->getResponse()
->setHeader('Content-Disposition:inline', ';filename=result.pdf')
->setHeader('Content-Type', 'application/x-pdf');

