0

I have a csv file that is generated on my backend, and the API requires I do a POST to trigger the generation of the file. Which is slightly problematic as is. Fortunately kinda, I stumble across a mix-match of methods that seems to be the general play to use for something like this by a good handful of people. Unfortunately I this doesn't appear to work on Firefox, and maybe other browsers. I know it works perfect in Chrome but I am wondering if its chrome that it will work in or if its just not working in firefox for some reason. Below is the code I have thus far, I've manually changed a couple variables trying to make sense of them here. But anyone have any ideas? Is this something that just can't be done?

    $.ajax({
        url: '/api?id=' + post_file_id,
        "async": true,
        "crossDomain": true,
        "method": "GET",
        "headers": {
            "cache-control": "no-cache",
        },
        "processData": false,
    }).done((response) => {
        let a = document.createElement("a");
        document.body.appendChild(a);
        a.style = "display: none";

        let blob = new Blob([response], {type: "text/csv;charset=utf-8;"});
        let url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = 'download-' + post_file_.replace(' ', '_') + '.csv';
        a.click();
        window.URL.revokeObjectURL(url);
        //document.body.removeChild(a);

    }).fail(() => {
        this._showStatusAlertBox('error', 'There is no data for post.');
    });
}
chris
  • 36,115
  • 52
  • 143
  • 252
  • Similar http://stackoverflow.com/questions/30694453/blob-createobjecturl-download-not-working-in-firefox-but-works-when-debugging – Musa Jun 29 '16 at 11:53

1 Answers1

0

Maybe you need some delay. Try with:

a.click();
setTimeout(function(){
   document.body.removeChild(a);
   window.URL.revokeObjectURL(url);  
}, 100);
pmiranda
  • 7,602
  • 14
  • 72
  • 155