1

I'm having a problem specific to IE with this function:

function downloadFileFromUserControl(filename) {
    var name = filename.split("/");
    var fName = name[name.length - 1];
    console.log("IE speaking, I'm just going to take a little nap during this request");
    var link = document.createElement('a');
    link.download = fName;
    link.href = filename;
    link.click();
    console.log("Oh, I'm late to the party?? Back to sleep I guess");
}

In Chrome this function works perfectly, I use this to download PDF files from hyperlinks. No errors, it just will post to the console.

Is there some additional code IE needs to make this work? Thanks

TopBanana9000
  • 818
  • 2
  • 14
  • 32
  • What exactly is going wrong? Are you getting an error? – Mike Cluck Apr 15 '16 at 15:42
  • 3
    It wouldn't surprise me if a DOM element in IE needs to be inserted into the DOM in order to process a click event or if you need to do the click like `setTimeout(function() {link.click()}, 20);` or both. – jfriend00 Apr 15 '16 at 15:45
  • It does run the function, but no error. It does nothing but post to the console – TopBanana9000 Apr 15 '16 at 15:47

2 Answers2

0

After the element is created, use the element.appendChild() or element.insertBefore() method to insert it to the document.

It is how it works, refer w3c reference

Sandeep Sukhija
  • 1,156
  • 16
  • 30
  • 1
    Trying this gives me a script hierarchy error. This case is sort of similar to mine: http://stackoverflow.com/questions/23071968/jquery-throws-script5022-hierarchyrequesterror-when-attempted-to-append-a-jquer . This is a webforms app (it's maintenance, leave me alone!). This JS function is on my 'Master' page. The function is being called from a user control (.ascx) file. – TopBanana9000 Apr 15 '16 at 15:58
  • Ok, so have you tried the solution provided in the similar question mentioned by you – Sandeep Sukhija Apr 15 '16 at 16:08
0

IE does NOT support the "download" attribute only newer browsers ie. Edge, you need to be careful with using new HTML5 functionality in old browsers, most of the time you need to add some sort of "fallback" for this particular case I'd suggest:

    if (typeof link.download !== typeof undefined && link.download !== false) {
       var message = document.getElementById('Some <Span> ID Here').innerHTML('Right-click and select "Download Linked File"');
    }
rojobo
  • 476
  • 4
  • 16