Jump to content

Wrong header sent if template caching enabled


kixe
 Share

Recommended Posts

Hi there,

I am using the following template file to provide a pdf download, of a pdf stored in a file-field.

<?php
$file = $page->pdf->filename;
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);

If template caching is disabled, everything working as expected and the following header is sent

HTTP/1.1 200 OK
Date: Fri, 26 Feb 2016 13:03:27 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Disposition: attachment; filename="anyfile.pdf"
Content-Length: 909233
X-XSS-Protection: 1; mode=block
Keep-Alive: timeout=2, max=1000
Connection: Keep-Alive
Content-Type: application/pdf

Using Template cache the header is overwritten by

HTTP/1.1 200 OK
Date: Fri, 26 Feb 2016 13:06:11 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-XSS-Protection: 1; mode=block
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=2, max=1000
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

Any ideas to prevent this?
 

Link to comment
Share on other sites

It works using wireSendFile(). New template:

<?php
/**
 * using this template allows you to use template cache
 *
 */
$file = $page->pdf->filename;
$disposition = 'inline'; // change to 'attachment' to provide download
$headers = array(    
    "content-length" => filesize($file),
    "content-disposition" => 'inline; filename="'.basename($file).'"'
);
wireSendFile($file, array('forceDownload' => false), $headers);

Define any headers to set new or overwrite default. "Force download 'must be set to false to avoid that 'Content-Disposition' is overwritten due to file extension.

But I am not sure if this uses the cache. Maybe its useless anyway it takes the same time to render.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...