Generating download file on for firefox

899 views
Skip to first unread message

Nathan Hüsken

unread,
Aug 29, 2015, 7:18:34 AM8/29/15
to HaXe Mailinglist
Hey,

It is kind of a javascript, not a haxe question. But maybe someone here
has the experience.

I am trying to generate a file on the fly and popup the download window
in a haxe/js project. Googling and trying around I came up with this
solution:

var anchor = js.Browser.document.createElement('a');
var blob = new Blob([a.getResultAsCSV()], {type : 'text/csv'});
anchor.setAttribute("href",URL.createObjectURL(blob));
anchor.setAttribute("download","daten.csv");
anchor.dataset.downloadurl =
['text/csv',anchor.getAttribute("download"),anchor.getAttribute("href")].join(":");
anchor.click();

This works in chrome! But does nothing in firefox. Annoying! :-).

Has anyone an Idea how I could make this work in firefox?

Thanks!
Nathan

Andreas Mokros

unread,
Aug 29, 2015, 1:08:51 PM8/29/15
to haxe...@googlegroups.com
Hi.

On Sat, 29 Aug 2015 13:18:13 +0200
Nathan Hüsken <nathan....@posteo.de> wrote:
> Has anyone an Idea how I could make this work in firefox?

Maybe like this?
https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events#Triggering_built-in_events

--
Mockey

Confidant

unread,
Aug 30, 2015, 4:47:16 PM8/30/15
to Haxe
Because only IE10+ supports blobs, the few times I've had to do this I've found it to be much more browser-compatible to send the file to the server and then have the server send it back with the correct MIME type.

Nathan Hüsken

unread,
Aug 31, 2015, 12:43:21 PM8/31/15
to haxe...@googlegroups.com
Hey,

Thanks for the replies.
I found the answer (ok, a answer) on stack-overflow. I have to add the
anchor to the DOM, so the code changes to this:

var anchor : Element = js.Browser.document.createElement('a');
var blob = new Blob([a.getResultAsCSV()], {type : 'text/csv'});
anchor.setAttribute("href",URL.createObjectURL(blob));
anchor.setAttribute("download",a.type+".csv");
anchor.dataset.downloadurl =
['text/csv',anchor.getAttribute("download"),anchor.getAttribute("href")].join(":");
js.Browser.document.body.appendChild(anchor);
anchor.click();
anchor.remove();

Best,
Nathan
Reply all
Reply to author
Forward
0 new messages