Skip to content

March 1, 2011

Force downloading XML files on IE

by Joe Kuan

I have this old code for downloading files using hidden IFRAME. Strangely, it just doesn’t work on IE when downloading XML files, it works on anything other than .xml file extension. Making the hidden IFRAME visible shows that IE automatically opens the XML file within the IFRAME, even the HTTP MIME header is ‘application/force-download’ (I have also tried ‘download, x-download’) on the server side.

Interestingly, the solution is in the Content-Disposition HTTP header. The typical use of this header is to specify filename of the downloaded content. However, according to the RFC 2183, it supports more options. The option relate to this problem is the disposition-type which can be: inline or attachment. Referring to RFC section 2.2, the definition for Attachment Disposition Type is

Bodyparts can be designated `attachment’ to indicate that they are separate from the main body of the mail message, and that their display should not be automatic, but contingent upon some further action of the user.

Whereas the Inline Disposition Type is described as

A bodypart should be marked `inline’ if it is intended to be displayed automatically upon display of the message.

which is what I encountered.

By specifying attachment, the download button behaves consistently.

<?php
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"{$_GET['download']}\"");
header("Content-length: " . filesize($filepath));
header("Cache-control: no-cache");
readfile($filepath);
?>

So in a nutshell, the default disposition type for Safari and Firefox is attachment. For IE, it depends.

Read more from Browser/IE, PHP, Programming, Web

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: