AngularJS:发送文件以在Firefox中下载 [英] AngularJS: Send files to download in Firefox

查看:72
本文介绍了AngularJS:发送文件以在Firefox中下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击一个按钮时,我正在尝试下载多个PDF文件.我当前的代码在Chrome中可以正常运行,但无法在Firefox中运行.

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.

在Firefox中是否有任何变通办法?

Are there any workarounds to make this work in Firefox?

推荐答案

我使用了iframe进行下载.只需将网址传递给此函数即可下载.

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++;
    };

然后您可以以

    $scope.downloadUrl('/data/url/goes/here');

这篇关于AngularJS:发送文件以在Firefox中下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆