Niklas Hantke
Niklas Hantke

Reputation: 363

Force file to download with IE

I have to open a file via a HTML-link. The file's location is on another computer and adressed by the IP adress (passing through a custom server tool) EG:

<a href="http://localhost:PORT/FILE.dotx" download>Download</a>

This works in firefox and chrome just fine, but IE (Version 8) interprets the file as a xml-File and tries to open it directly.

There is no possibility to upgrade or change the client's browser.

Is it possible to force IE to download the file without using PHP, VB or Rails? (as we don't have a apache server or something like this)

Upvotes: 2

Views: 1789

Answers (1)

Zdenek
Zdenek

Reputation: 710

lighttpd.conf sample, requires mod_setenv:

$HTTP["url"] =~ "\.pdf\?dl$" {
    setenv.add-response-header  = ( "Content-Disposition" => "attachment")
}

This is the only reliable way. IE won't trust MIME types because of an old Apache flaw workaround where Apache sent wrong MIME types and Microsoft "fixed it".

While all pdf files could simply be given the downloading header, I have chosen to show a neater way - only the parameter ?dl activates this behavior. Plain pdfs will still display in-browser and only a link which has ?dl appended gets the special treatment.

I am actually using this technique on my server, but it is implemented in php because I can't make do with the static handlers alone. Since I offer images through this, I also add the Cache: no-transform header to prevent Opera Turbo from recompressing the file to be downloaded.

EDIT: Fixed the Disposition word - has to be capitalized to also work in Webkit-based browsers.

Upvotes: 1

Related Questions