23
<a href="path/to/file/filename.xxx" download="filename.xxx">filename</a>'

When i click the link, my filename.xxx should be downloaded.

It works perfectly in chrome. But in Internet explorer, it opens the file instead of downloading. What could be the problem? Is there any properties that is to be added to make it work in ie.

And also i need a file download sample that works for all the browsers.

Ebenezar John Paul
  • 1,528
  • 5
  • 19
  • 39
  • what extension is it? – reyaner Jul 31 '13 at 11:18
  • 1
    http://stackoverflow.com/questions/1597732/php-force-file-download-and-ie-yet-again already answered this particular question – Bruce BENAMRAN Jul 31 '13 at 11:19
  • @BruceBENAMRAN: That has nothing to do wth the download attribute which is what the user is trying to use. – Chris Jul 31 '13 at 11:22
  • @reyaner : any extension. i am dynamically uploading files and saves them in this format and showing the file to the user as above. when the user clicks it, it should get downloaded irrespective of the extension (as it does in chrome). – Ebenezar John Paul Jul 31 '13 at 11:27
  • I have done the same but it doesn't download the file. https://stackoverflow.com/questions/46876439/excel-file-download-failed-in-chrome-using-php-yii2 – Moeez Oct 22 '17 at 16:24

8 Answers8

20

The download attribute is not supported in IE (see http://caniuse.com/#search=download%20attribute).

That suggests the download attribute is only supported by firefox, chrome, opera and the latest version of blackberry's browser.

For other browsers you'll need to use more traditional methods to force download. That is server side code is necessary to set an appropriate Content-Type and Content-Disposition header to tell (or trick depending on your point of view) the browser to download the item. Headers should look like this:

Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"filename.xxx\"

(thanks to antyrat for the copy and paste of the headers)

EricLaw
  • 54,427
  • 7
  • 140
  • 182
Chris
  • 26,164
  • 4
  • 67
  • 85
11

It should be fixed on server side. Your server should return this headers for this file types:

Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"filename.xxx\"
antyrat
  • 26,266
  • 9
  • 69
  • 74
  • This can only be applied if the user has Access to the IIS servers on which he has his site hosted. – AnaMaria Jul 31 '13 at 11:24
  • 2
    @AnaMaria yeah, but otherwise this problem can't be resolved for all user at same time... – antyrat Jul 31 '13 at 11:25
  • 1
    octet-stream is maybe to broad, try setting the correct header : http://www.iana.org/assignments/media-types/media-types.xhtml – TecHunter Mar 30 '15 at 13:51
1

For apache2 server:

AddType application/octect-stream .ova

File location will depend on particular version of Apache2 -- ours is in /etc/apache2/mods-available/mime.conf

Reference:

https://askubuntu.com/questions/610645/how-to-configure-apache2-to-download-files-directly

gerardw
  • 4,437
  • 33
  • 33
0

This must be a matter of http headers.

see here: HTTP Headers for File Downloads

The server should tell your browser to download the file by sending

Content-Type: application/octet-stream; 
Content-Disposition: attachment;

in the headers

Community
  • 1
  • 1
Armel Larcier
  • 14,350
  • 7
  • 62
  • 86
0

This is not a code issue. It is your default IE settings

To change the "always open" setting:

  1. In Windows Explorer, click on the "Tools" menu, choose "Folder options"
  2. In the window that appears, click on the "File Types" tab, and scroll through the list until you find the file extension you want to change (they're in alphabetical order). For example, if Internet Explorer always tries to open .zip files, scroll through the list until you find the entry for "zip".
  3. Click on the file type, then the "Advanced" button.
  4. Check the "Confirm after download" box, then click OK > Close.

EDIT: If you ask me , instead of making any changes in the code i would add the following text "Internet Explorer users: To download file, "Rightclick" the link and hit "Save target as" to download the file."

EDIT 2: THIS solution will work perfectly for you. Its a solution i just copied from the other answer. Im not trying to pass it off as my own

Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"filename.xxx\"

However you must make sure that you specify the type of file(s) you allow. You have mentioned in your post that you want this for any type of file. This will be an issue.

For ex. If your site has images and if the end user clicks these images then they will be downloaded on his computer instead of opening in a new page. Got the point. So you need to specify the file extensions.

AnaMaria
  • 3,130
  • 3
  • 16
  • 36
0

Zip your file (.zip) and IE will give the user the option to open or download the file.

Jason
  • 11
  • 5
0

It is known HTTP headers problem with Internet Explorer. Try to edit your server's .htaccess file (if you use Apache) and include the following rules:

# IE: force download of .xxx files
AddType application/octect-stream .xxx
<Files *.xxx>
  ForceType application/octet-stream
  Header Set Content-Disposition attachment
</Files>
Bud Damyanov
  • 24,155
  • 6
  • 37
  • 49
-3

You could configure this in your http-Header

httpResponse.setHeader("Content-Type", "application/force-download");
        httpResponse.setHeader("Content-Disposition",
                               "attachment;filename="
                               + "MyFile.pdf"); 
Stimpson Cat
  • 1,131
  • 13
  • 37
  • Your answer doesn't reference the language or platform this works with. I tried it in JavaScript and httpResponse isn't defined. – Kevin Ghadyani Jan 05 '16 at 04:00
  • Then defnine it. It is configured in the header. It is not depending on the language http is a protocol, dude. That does not care for programming languages or platforms. – Stimpson Cat Jan 08 '16 at 13:51