Download attribute not working in Firefox
Asked Answered
W

1

22

I'm trying to let the user download some data as a CSV (text) file, using Javascript and the HTML5 Download attribute (http://caniuse.com/#feat=download).

The data is formed in an array, and then added to a new Blob object.

It works perfectly in Chrome and Opera, but does not work at all in Firefox.

Original example I am attempting to copy: http://blog.eliacontini.info/post/79860720828/export-to-csv-using-javascript-the-download-attribute

Fiddle: http://jsfiddle.net/8wos7cf8/5/

Javascript:

$('#downloadButton').click(function () {
// some data to export
var data = [{
    "title": "Book title 1",
    "author": "Name1 Surname1"
}, {
    "title": "Book title 2",
    "author": "Name2 Surname2"
}, {
    "title": "Book title 3",
    "author": "Name3 Surname3"
}, {
    "title": "Book title 4",
    "author": "Name4 Surname4"
}];

// prepare CSV data
var csvData = new Array();
csvData.push('"Book title","Author"');
data.forEach(function (item, index, array) {
    csvData.push('"' + item.title + '","' + item.author + '"');
});

// download stuff
var fileName = "data.csv";
var buffer = csvData.join("\n");
var blob = new Blob([buffer], {
    "type": "text/csv;charset=utf8;"
});
var link = document.createElement("a");

if (link.download !== undefined) { // feature detection
    // Browsers that support HTML5 download attribute
    link.setAttribute("href", window.URL.createObjectURL(blob));
    link.setAttribute("download", fileName);
    link.click();
} else {
    alert('CSV export only works in Chrome, Firefox, and Opera.');
}
});

HTML:

<div class="toggle-button" id="downloadButton"><span>Export to CSV</span></div>

When I add an alert with:

alert(window.URL.createObjectURL(blob));

I get this result in Firefox:

Firefox blob

...and this result in Chrome/Opera:

Chrome blob

So it seems like it omits the URL path in Firefox for some reason.

Washwoman answered 24/11, 2014 at 22:56 Comment(1)
Regarding mpeg and probably other files, if the Firefox player opens, it's because your need in about:config the setting media.play-stand-alone=false. It may be media.windows-media-foundation.enabled on Windows.Kalat
B
80

You might try adding the element to the DOM before triggering the click:

document.body.appendChild(link);
link.click();
document.body.removeChild(link);

This worked for me in Firefox 34

jsfiddle: http://jsfiddle.net/8wos7cf8/7/

Breskin answered 25/11, 2014 at 0:11 Comment(6)
Thank you jmfolds . This is how to get the 'link' attribute of the 'a' element working in Firefox. Very helpful!Bromism
This worked great for FF but broke IE... it makes my browser go to /true. May have to adjust this to check for IERoulers
@MattHintzke - HTML5 Download attribute is not supported in IE or Safari. caniuse.com/#feat=downloadWashwoman
Thanks a lot. Now works for me in Safari, Firefox, Chrome and Opera.Cesaro
i dont need to call appendChild, removeChild on Chrome, but it is necessary on FirefoxShrunk
Fixed my issue with Firefox compatibility.Anderton

© 2022 - 2025 — McMap. All rights reserved.