Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE download file

Using the following lines of code I am able to download a file in the response of a Ajax call in Firefox, Chrome, Opera. However in IE the href attribute download is not supported. Therefore the below does not work in IE.

HTML:

 <div class="fRight" style="margin-left:5px; margin-rigth:5px" >
    <input type="button" value="Request File"  id = "chReqFileBtn"  onclick = "handleClick"/>
    <a href="#" id="challengeReqFileAnchor" style="visibility: hidden"></a>
 </div>

JavaScript:

function handleClick()
{
    var code = $('#code').val();
    var quantity = $('#quantity').val();

    var req = $.ajax(
    {
        'type': 'POST',
        'url' : $apiBasePath+'config/challenge-file',
         contentType : 'application/json',
        'data': JSON.stringify({'code':code, 'quantity':quantity}),
        'success':function(response, status, xhr)
        {
            var code = xhr.getResponseHeader('Operation-Code');

            var anch = $('#challengeReqFileAnchor');
            anch.attr(
            {
                "download" : 'request.bin',
                "href" : "data:text/plain," + response       
            });
            anch.get(0).click();
        },
        'error': function(request,status,errorThrown) 
        {
           ......
        }
    });
    $pendingReqs.push(req);  
}

What options would I have to accomplish the same behavior in IE as well?

like image 494
steve Avatar asked Aug 04 '14 14:08

steve


People also ask

How do I download files from Internet Explorer?

Open Internet Explorer, select the Tools button, and then select View downloads. In the View Downloads dialog box, select Options in the lower-left. Choose a different default download location by selecting Browse and then selecting OK when you're done.

How do I make Internet Explorer 11 automatically download files?

File Download Settings Select Tools > Internet Options. Click the Security tab. Click Custom Level. About 1/3 of the way down the page, enable automatic prompting for file downloads and File download.

Why Will Internet Explorer not download files?

Cause. To download files, Internet Explorer must create a cache or temporary file. In Internet Explorer 9 or a later version, if the file is delivered over HTTPS, and any response headers are set to prevent caching, and if the Do not save encrypted pages to disk option is set, a cache file is not created.


2 Answers

Download attribute is NOT supported in IE and iOS Safari

enter image description here

IE<10:

Command SaveAs using execCommand can do the trick, by making element's contents downloadable.

Cons:

  • Problems in some versions of IE running on Win7 [I don't know if it's fixed Here]
  • Need a DOM element to contain data

IE>=10

Using msSaveBlob, it's a method that allows to save Blob or File by sending this headers:

Content-Length: <blob.size>
Content-Type: <blob.type>
Content-Disposition: attachment;filename=<defaultName>
X-Download-Options: noopen

Check Saving files locally using Blob and msSaveBlob

Cons:

  • Need a to define a Blob

Other browsers

If you don't want to implement all that by yourself, there is a nice library FileSaver.js to save generated files on client side. It supports Firefox, Chrome, Chrome for Android, IE 10+, Opera and Safari. For IE<10 there is a fork of the library that adds saveTextAs to save text files (.htm, .html, .txt)

Cross browsers solution

A server side script that receives filename, data, meme type then send the file with the header Content-Disposition: attachment; filename=FILENAME

like image 92
Issam Zoli Avatar answered Oct 13 '22 03:10

Issam Zoli


I think this is related to the anch.get(0).click(); not supported by all browser specially for hidden anchors, so you may try following code instead,

anch.get(0).show().focus().click().hide();
like image 24
Dharmesh Patel Avatar answered Oct 13 '22 02:10

Dharmesh Patel