簡體   English   中英

Zend Framework 如何設置標題

[英]Zend Framework how to set headers

我有一個問題,我該怎么做:

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

使用 Zend Framework,我嘗試過:

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

但不能正常工作。

您設置響應標頭的語句格式略有錯誤:

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

以上應該有效 - 請注意Content-Disposition -header 中的差異。

順便說一下......當你想強制下載框(而不是在瀏覽器中加載文檔)時,你應該使用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 正如我在評論中所寫,有時緩存標頭可能會干擾您的下載。 檢查發送了哪些緩存標頭。

晚了,我可以推薦這個動作助手作為一個簡單的、可重用的組件,用於將文件或內存中的數據發送到瀏覽器。

具有用於緩存、處置的選項並且可以利用 Apache Sendfile

我的猜測是你正在做類似的事情:

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

或者其他的東西。

此處的響應將永遠不會呈現(呈現標題)。 通常在操作后打印期間呈現響應。

您將必須直接設置標頭(如您在非 oo 代碼中所述),或直接使用$this->getResponse()->sendHeaders()

我有一個標題集。 它沒有設置,但已添加。 所以我的內容類型是 text/html 和 application/pdf。

將 Content-Type 標記為 TRUE 可以在 IOS 和其他設備中下載,這些設備在下載或錯誤后僅顯示神秘符號:

->setHeader('Content-type', 'application/x-pdf', true);

setHeader($name, $value, $replace = false)

來自: https ://framework.zend.com/manual/1.12/de/zend.controller.response.html

解決了

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM