Zend Framework how to set headers

前端 未结 5 1791
天命终不由人
天命终不由人 2020-12-13 09:08

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

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


        
相关标签:
5条回答
  • 2020-12-13 09:42

    I had a header set. It was not set, but ADDED. So I had a Content-Type of text/html and also 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:

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

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

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

    0 讨论(0)
  • 2020-12-13 09:46

    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.

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

    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.

    0 讨论(0)
  • 2020-12-13 09:47

    Solved

            $this->getResponse()
             ->setHeader('Content-Disposition:inline', ';filename=result.pdf')
                 ->setHeader('Content-Type', 'application/x-pdf');
    
    0 讨论(0)
  • 2020-12-13 09:58

    Late to the table, I can recommend this action helper as a simple, reusable component for sending files or in memory data to the browser.

    Has options for caching, disposition and can utilise Apache Sendfile

    0 讨论(0)
  • 2020-12-13 10:08

    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.

    0 讨论(0)
提交回复
热议问题