Page 1 of 1

Force Download in IE8, is it impossible?

Posted: Sun Jun 14, 2009 3:24 pm
by Eric!
Hi,

I'm getting desperate. I've tried an amazing combination of header commands to get IE8 to prompt a file dialog box for a known or unknown MIME type.

I've spent a lot of time researching this and I still can't get anything that IE8 likes without hanging. My code works fine in Google Chrome, Mozilla and Firefox...

Does anyone know how to get this to work in IE8?

I've tried this:

Code: Select all

 
header("Content-Type: application/pdf");
header("Content-Length: ".(string)($filesize));
header("Content-Disposition: attachment; filename=\"".$filename."\";");
 
I've tried the more complicated:

Code: Select all

 
    header("Pragma: public");
    header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+4, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);   
    header("Content-Type: application/pdf");
    header("Content-Length: ".(string)($filesize));
    header("Content-Description: File Transfer");
    header("Accept-Ranges: bytes");
    header("Content-Disposition: attachment; filename=\"".$filename."\";");
    header("Content-Transfer-Encoding: binary\n");
 
I've tried numerous variation on Cache-Control, Content-Types, and Expires, but nothing will make IE8 happy. Does anyone have a clue? Many of the examples I found on the web also fail to work with IE8 and hang it.

Thanks in advance!
Eric

EDIT: I've found a MS note about IE7 and IE8 having a new "X-Download-Options: noopen" for security. However I've tried their exact syntax for Force Download and it doesn't work either.

http://blogs.msdn.com/ie/archive/2008/0 ... ction.aspx

Re: Force Download in IE8, is it impossible?

Posted: Mon Jun 15, 2009 12:30 am
by Weirdan
So it's not popping the download box, but what does it do instead?

Re: Force Download in IE8, is it impossible?

Posted: Mon Jun 15, 2009 12:57 am
by omniuni
Try disabling the (I suspect installed) Adobe Reader plugin.

Re: Force Download in IE8, is it impossible?

Posted: Mon Jun 15, 2009 10:16 am
by Eric!
Ok, I should mention this is http not https.

The forced download should work whether the mime type is recognized (adobe reader plugin present) or not.

Some combinations hang IE8 completely and the best I've managed is to get IE8 to load and display with the plugin, but no forced save. Even turning off mime sniffing and using the stupid 'noopen' line doesn't work. I copied the MS example and it didn't work either. I'm so desperate I even reinstalled IE8. (remember the same headers work on other browsers)

Re: Force Download in IE8, is it impossible?

Posted: Mon Jun 15, 2009 3:58 pm
by Eric!
I've put up a test case. Can someone check it with IE8?

It uses the following headers:

Code: Select all

Pragma: public
Expires: Mon, 15 Jun 2009 23:59:35 GMT
Last-Modified: Mon, 15 Jun 2009 19:59:35 GMT
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Content-Type: application/pdf
Content-Description: File Transfer
X-Download-Options: noopen
X-Content-Type-Options: nosniff
Content-Disposition: attachment; filename="testdocument.pdf";
Content-Transfer-Encoding: binary
Content-Range: bytes 0-5937/5938
Content-Length: 5938
You should be prompted to save a test pdf file. The script that produces the above headers is located at http://www.sailsarana.com/guide/test on my site here and here is the code. (note the function head($string) is just for debugging).

Code: Select all

 
<?php
    $basename = './';
    $b_filename="testdocument.pdf";
    $fullname = "$basename$b_filename";
    $filesize = filesize($fullname);
 
    //Begin writing headers
    head("Pragma: public");
    head("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+4, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
    head("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    head("Cache-Control: no-store, no-cache, must-revalidate");
    head("Cache-Control: post-check=0, pre-check=0", false);   
    head("Content-Type: application/pdf");
    head("Content-Description: File Transfer");
    head("X-Download-Options: noopen"); // IE7 & IE8?
    head("X-Content-Type-Options: nosniff"); // IE8 bonus?
    head("Content-Disposition: attachment; filename=\"".$b_filename."\";");
    head("Content-Transfer-Encoding: binary");
 
    $size=$filesize;
    //check if http_range is sent by browser (or download manager)
    if(isset($_SERVER['HTTP_RANGE'])) {
        list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']);
        //if yes, download missing part
        str_replace($range, "-", $range);
        $size2=$size-1;
        $new_length=$size2-$range;
        head("HTTP/1.1 206 Partial Content");
        head("Content-Length: $new_length");
        head("Content-Range: bytes $range$size2/$size");
        $stat="Resumed ";
    } else {
        $size2=$size-1;
        head("Content-Range: bytes 0-$size2/$size");
        head("Content-Length: ".(string)($size));
        $range=0; //fseek to start of file
        $stat="New ";
    }
    //open the file
    $fp=fopen("$fullname","rb") or die("File open error.");
    //seek to start of missing part or beginning
    fseek($fp,$range);
 
    //start buffered download and abort if lost connection
    while(!feof($fp) and (connection_status()==0))
    {
        //reset time limit for big files
        set_time_limit(0);
        print(fread($fp,1024*8));
        flush();
        ob_flush();
    }
    fclose($fp);
    exit;
function head($string)
{
  $debug=0;
  if($debug) echo $string."<br>";
  else header($string);
}
?>
I tested this particular case in Google Chrome and IE8. They all work in Google, but IE 8.0.6001.18702 shows a small icon in the bottom status bar showing an arrow and then the word done. However the browser totally locks up while the circle keeps spinning (connecting...) and I have to use task manager to kill it.

It will be interesting to see how these work for someone else. :banghead:

Re: Force Download in IE8, is it impossible?

Posted: Mon Jun 15, 2009 4:47 pm
by Eric!
Ok the problem turned out to be something in the IE8 configuration (eventhough I just downloaded it). By doing the tools-internet options-advanced and RESET, everything seems to be working on IE as it should be with the scripts. I don't know what in the configuration is causing it to hang, but at least it isn't my code....

Re: Force Download in IE8, is it impossible?

Posted: Mon Jun 15, 2009 8:05 pm
by omniuni
It's IE. That's all you need to know. :?

Re: Force Download in IE8, is it impossible?

Posted: Tue Jun 16, 2009 1:05 pm
by Eric!
I suspect it isn't playing well with a previously installed plugins. After going from IE7 to 8 I was surprised that I didn't need to change or redownload any of them. But obviously something is wrong with how IE8 configured itself....


UPDATE: It turns out the Download Accelerator Program (DAP) was not working right with IE8. Uninstalling the plug-in and installing the newer version stopped IE's erratic behavior even though it wasn't enabled. It was weird.