Zend Framework 如何设置标题

2021-12-29 00:00:00 php zend-framework

我有一个问题,我该如何做这样的事情:

I have a question, how can I do something like this:

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

使用 Zend Framework,我已经尝试过:

With Zend Framework, I have tried:

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

但不能正常工作.

推荐答案

您设置响应标头的语句有点格式错误:

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

以上应该可以工作 - 请注意 Content-Disposition-header 中的区别.

The above should work - please note the difference in the Content-Disposition-header.

顺便说一句...当您想强制下载框(而不是在浏览器中加载文档)时,您应该使用 Content-Disposition attachment.

By the way... When you want to force a download box (instead of loading the document in the browser) you should use the Content-Disposition attachment.

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

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

Depending on the browser it may be possible that you also have to set the Content-Length or change the Content-type to a combination (multiple headers) of one or more of application/force-download, application/octet-stream and/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.

相关文章