IE ignores octet-stream - how to force download? - pdf-generation

I'm using wkhtmltopdf to generate PDF files. This program accepts one of two ContentType values, 'application/pdf' and 'application/octet-stream'. I want the PDF files generated to be saved to the user's machine, so I set it to 'application/octet-stream'. Chrome prompted the download, just as expected. So did FireFox. Of course Internet Explorer ignored this setting and opened the PDF file in a browser window instead. I'm using Internet Explorer 8 and 9 for testing this. I don't have access to the server, and I don't want to change how all PDF files are handled in any case. I want this application to prompt the user to download the file, and need to know how to force IE to do this instead of ignoring the ContentType value.
I used Fiddler2 to check out how the ContentType is being reported in IE and Chrome, and both show 'application/octet-stream', so I'm not sure why IE is insisting on rendering the PDF in a browser. Any advice?

PHP CODE:
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"filename\"");
This should work.

Related

How to output a file from CodeIgniter without forcing download?

There seem to be lots of solutions out there for CodeIgniter to force download of a file, but I can't find any for outputting a file without forcing a download.
In my case, I've redirected my file downloads through a controller (originally Apache handled them directly) in order to log statistics. I used the CodeIgniter force_download function in the download helper. However, I've now had a request that some or all files should be able to be opened in the browser, such that saving to a file isn't forced, so I'm wanting to know how to do it.
Presumably, it's mostly a matter of outputting the right header, though I don't know whether different headers will be required for different file types. Has anyone got an example?
You just have to specify the good mime-type and omit Content-Disposition: attachment.
So if the browser can render it, it will be display. If it can't, it will be download.
example:
$file = 'images/smileys/panda.gif';
if (file_exists($file))
{
$finfo = new finfo(FILEINFO_MIME);
header('Content-Description: File Transfer');
header('Content-Type: '.$finfo->file($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}

IE8 :Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found

This should be a old problem about IE8 download problem. I used PHP to set the response header like:
header("Pragma: public");
header("Expires: 0");
header("Content-type: application/octet-stream");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: ".strlen($content));
header("Content-Disposition: attachment; filename='$filename'");
and the Connection is close capture by Fiddler.
When I try to download the file with above header in IE8 (not in SSL), the message box :
"Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found." pop up and download is stopped.
I searched on net and try lots of solutions but seems none of them works. Would there be any other solution for this problem?
PS: I have tried: Delete response header/reset contentType in header/ Add the site into trusted site/ set "Do not save encrypted pages to disk" as true.
Thanks.
In addition to Le-roy's answer:
I set
Cache-Control: private, max-age=1
and that fixed the issue for me.
From Le-roy's link, if any of these are set:
Cache-Control header with the tokens no-cache, no-store
Vary header that specifies almost anything
Pragma header that specifies exactly no-cache
IE download will fail.
IE has problems with downloading files from HTTPS when no-cache is set in the header. Try this page http://blogs.msdn.com/b/ieinternals/archive/2009/10/02/internet-explorer-cannot-download-over-https-when-no-cache.aspx for more info.

Audio tag progress bar standing on right

I'm having an issue with audio tag and Firefox. I am using the default browsers' controls. It works fine with Chrome and Opera, but in Firefox progress bar stays on right(like it was the end of the record) - the sound plays normally, but I can't seek through the file. What did I do wrong?
Here's how I send my file to browser.
header('Content-Description: File Transfer');
header('Content-Type: audio/wav');
header('Content-Disposition: attachment; filename=' . basename($clientName));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
And my use of audio tag:
<audio preload="auto" controls="controls" src="/record/download/id/55" />
Thanks in advance for your answers.
Firefox will send range requests (with the "Range" request header) and your server code should respond accordingly (with "206 Partial Content" status). When the server doesn't appear to understand range request firefox will assume it's some kind of a live stream and behave in the described manner.
If your code runs through an apache server you can have it handle range requests for you, for instance by using mod_xsendfile. Another option is to implement is yourself. For details see RFC 2616, especially section 10.2.7 and 14.35.

Firefox, PHP Headers and application/octet-stream

We have a page that outputs a file using PHP headers, so when you visit the page the download prompts up.
Randomly, the page will return a file application/octet-stream, other times it will return the correct thing.
It works prefectly in Chrome, IE, etc.
This only happens in Firefox and it is completely random.
Basically, it outputs as a application/octet-stream instead of the correct one.
After some more digging, I found the following:
http://support.mozilla.com/en-US/questions/800957
http://support.mozilla.com/en-US/questions/746116
http://support.mozilla.com/en-US/questions/699834

How to Prevent Caching When using PDF Streaming with Acrobar Reader 10.0 (HTTP1.0/HTTP1.1)

I was trying to find a way to prevent browsers from caching PDF that is being loaded using a streaming methods.
FireFox and Chorme deals just fine with the following headers and doesn't cache any pdf file:
Response.AddHeader("Pragma", "no-cache, no-store");
Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0");
Response.AddHeader("Expires", "-1");
Although, IE 7 (with acrobat reader 9.4.1) works only with the following headers and prevent the caching of the PDF doc:
Response.AddHeader("Pragma", "no-cache, no-store");
Response.AddHeader("Cache-Control", "private, must-revalidate, max-age=0");
Response.AddHeader("Expires", "-1");
When i was trying to use IE 7 with Acrobat Reader 10, the above header didn't make any different and cached the PDF no matter what i tried.
When i am trying to put Cache-Control: no-cache, no-store, the pdf was not loaded at all.
According to my understanding, IE use the cache mechanism to load the PDF documents.
Is there anyone familiar with a global or specific way (by using other headers for example) that can help prevent caching of PDF documents?
Add a random number to the URL, either in the path or in the query string. That way, it'll download the file every time. You could also change the number only, if the file has changed, for example using the mtime of the file.
PHP (since everybody understands that, even if nobody likes it):
Download PDF
This issue of showing PDF (and other document types) inline with the use of the no-cache header has been filed as a bug to Microsoft: http://support.microsoft.com/kb/316431. IE uses its own caching mechanism when reading PDFs inline.
Unfortunately, the folks at M$ said this "works as designed" and users should not use the no-cache header... go figure.
You could try VSU's idea of using a Java PDF reader... I may go this route too.
Controlling the cache settings down the pipe is not fool proof. The alternative is to encode the realtime and date in the file name of the PDF.
You can encode the time date into the filename of the PDF so that each time a request is made the filename is unique.
Response.AddHeader "Content-Disposition","attachment;filename=somename" + CurrentDate() + Currenttime() ".pdf"
CurrentDate adn CurrentTime are imaginary functions. You need to write that code.

Resources