niczak
niczak

Reputation: 3917

Serving Up PDF File Download Fails in IE7/8

I am using Zend and have some files outside of the webroot that I would like to be able to serve up. I have tried two approaches, both of which work in all browsers except for versions of IE 8 or lower.

The two (working) approaches that I have tried are the following:

  // Approach #1
  header('Content-Type: application/pdf');
  header("Pragma: ");
  header("Content-Disposition: attachment; filename=\"$filename\"");
  //header('Content-Transfer-Encoding: binary');
  header("Pragma: no-cache");
  header("Expires: 0");
  readfile($file);      

  // Approach #2
  $this->getResponse()
   ->setHeader('Content-Disposition', "attachment; filename=$filename")
  ->setHeader('Content-type', 'application/x-pdf');
  fpassthru($file);

Like I said, both approaches work in modern browsers (even IE9) but not in older versions of IE. The error I am getting is the following: http://cl.ly/image/1G3x370b1s09

I have looked into several posts on this topic and tried more different combinations of headers than I can even count. Is there a more bulletproof way of handling this functionality that wont cause issues with older browsers?

Thanks!

Upvotes: 1

Views: 661

Answers (3)

rb-cohen
rb-cohen

Reputation: 11

Follwing the advice at http://support.microsoft.com/default.aspx?scid=KB;EN-US;q316431&, these headers worked for me:

header("Cache-control: max-age=3600, must-revalidate");
header("Pragma: public");
header("Expires: -1");

I always get caught out by this! :(

Upvotes: 0

niczak
niczak

Reputation: 3917

Against my will I gave up on trying to fight with headers and completely changed the way I am handling file downloads. When a user requests a file now, it is temporarily hashed, copied to an area that the web-server can see, the user is redirect to that file and once they leave the download area the file is deleted. If they go inactive the file is deleted automatically at a set interval.

Thank you for all of the input kulishch and how ironic is it that you are from Minnesota as well!? Happy Holidays!

-- Nicholas

Upvotes: 0

kulishch
kulishch

Reputation: 178

I've fought with this before and I think it stems from caching headers.

There's three: Expires, Cache-Control (HTTP 1.1), and Pragma (HTTP 1.0). My experience has been the older versions of IE like to see all three of these headers. Try using the following prior to any other headers and content you send:

header("Cache-control: no-cache");
header("Pragma: no-cache");
header("Expires: -1");

This article from Microsoft goes in to more discussion about the caching headers.

This is what I have done in the past to get it to work:

$file = $fileInfo->openFile('r');
header("Pragma: public");
header("Cache-Control: public");
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="'.$file->getFilename().'"');
print $file->fpassthru()

Upvotes: 3

Related Questions