0

I am attempting to download several PDF files when clicking on a button. My current code is working fine in Chrome, but I cannot get it to work in Firefox.

angular.forEach(downloads, function(download) {

    $http.get(download.url, {responseType: 'arraybuffer'}).then(function(response) {
        var blob = new Blob([response.data], {type: "application/pdf"});

        var downloadLink = angular.element('<a></a>');
        downloadLink.attr('href',window.URL.createObjectURL(blob));
        downloadLink.attr('download', download.name);
        downloadLink[0].click();
    });
});

They are in the Network tab, however they are never being sent to download.

enter image description here

Are there any workarounds to make this work in Firefox?

Patrick Reck
  • 329
  • 2
  • 4
  • 16

2 Answers2

0

I have a similar Angular download and what's working here is:

var blob = new Blob([response.data], {type: "application/pdf"});
var blobURL = window.URL.createObjectURL(blob);
if (window.navigator.msSaveOrOpenBlob) {
    // of course, IE needs special hand-holding....
    window.navigator.msSaveOrOpenBlob(blob, 'something.pdf');
} else {
    window.open(blobURL);

Please make absolutely sure you are not blocking popups from the site, that has tripped me several times on various browsers.

Johannes Jander
  • 4,974
  • 2
  • 31
  • 46
0

I used an IFrame to download. Just pass an url to this function to download.

    $scope.downloadUrl = function (url) {
        var oIframe = window.document.createElement('iframe');
        var $body = $(document.body);
        var $oIframe = $(oIframe).attr({
            src: url,
            style: 'display:none;'
        });
        $body.append($oIframe);
        $scope.actions++;
    };

Then you can call as

    $scope.downloadUrl('/data/url/goes/here');
brewsky
  • 607
  • 1
  • 9
  • 15
  • This puts the contents in an iframe, but it doesn't make the browser want to download it – Patrick Reck Feb 16 '16 at 13:09
  • Firefox & IE do not support multiple downloads on the same page as Chrome does. I used the above codes for workaround on Firefox and it does work. See this post for more on downloading via iframe: [link]http://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery – brewsky Feb 17 '16 at 14:14