Zend 框架如何设置标头

2022-08-30 14:13:55

我有一个问题,我该怎么做:

header("Content-Disposition: inline; filename=result.pdf"); 
header("Content-type: application/x-pdf"); 

使用Zend框架,我尝试了:

        $this->getResponse()
        ->setHeader('Content-Disposition:inline', ' filename=result.pdf')
        ->setHeader('Content-type', 'application/x-pdf');

但无法正常工作。


答案 1

用于设置响应标头的语句格式略有错误:

$this->getResponse()
     ->setHeader('Content-Disposition', 'inline; filename=result.pdf')
     ->setHeader('Content-type', 'application/x-pdf');

上述内容应该有效 - 请注意 -header 中的区别。Content-Disposition

顺便一提。。。如果要强制使用下载框(而不是在浏览器中加载文档),则应使用 .Content-Dispositionattachment

$this->getResponse()
     ->setHeader('Content-Disposition', 'attachment; filename=result.pdf')
     ->setHeader('Content-type', 'application/x-pdf');

根据浏览器的不同,您可能还必须设置或更改为 一个或多个 、 和/或 的组合(多个标头)。正如我在评论中所写的那样,有时缓存标题可能会干扰您的下载。检查发送了哪些缓存标头。Content-LengthContent-typeapplication/force-downloadapplication/octet-streamapplication/download


答案 2

在表中,我可以推荐此操作帮助程序作为一个简单的,可重用的组件,用于将文件或内存中的数据发送到浏览器。

具有缓存,处置的选项,并可以利用Apache Sendfile


推荐