File Download on Javascript does not work on firefox

have written this piece of code which is working perfect on Google Chrome but not working on Firefox, do you have a clue?

The expected behavior is that you pass as parameters a xml text and a name and it downloads a xml file with that xml text and with the name you sent, as I said, for chrome is ok but for firefox, it does not download it.

/** * Creates and download a file from a selected row on query results * @param xmltext * @param filename */

function createAndDownloadFile(xmltext, filename){

var pom = document.createElement('a');
//creates a blob variable with the xml text
var bb = new Blob([xmltext], {type: 'text/xml'});

//sets the atribute href
pom.setAttribute('href', window.URL.createObjectURL(bb));
pom.setAttribute('download', filename);

//creates the download url
pom.dataset.downloadurl = ['text/xml', pom.download, pom.href].join(':');
pom.draggable = true; 
pom.classList.add('dragout');

//apply the click on to download the file
pom.click();

}

Upvotes: 1

Views: 1168

Answers (1)

MattSidor
MattSidor

Reputation: 2739

I had a very similar problem that was just answered for me here on stackoverflow: Download attribute not working in Firefox

Try adding the element to the DOM before the click event:

//apply the click on to download the file
document.body.appendChild(pom);
pom.click();
document.body.removeChild(pom);

Upvotes: 1

Related Questions