返回application / pdf时要使用的数据属性是什么?

时间:2015-05-29 01:11:37

标签: angularjs

我从web api方法返回一个字节数组,该方法将内容类型设置为' application / octet-stream。这个字节数组用于pdf文件。我正在使用angularjs。

问题:对于这种情况,下面代码中href的数据值是多少?我已将其设置为附件/ pdf,但无效。

 return reportsDataService.getMyData()
                    .then(function (data) {
                        var element = angular.element('<a/>');
                        element.attr({
                            href: 'data:attachment/pdf,' + url,
                            target: '_blank',
                            download: 'myreport.pdf'
                        })[0].click();
                    });

1 个答案:

答案 0 :(得分:1)

根据Blob文档,您可以按照以下代码进行编码

var blob = new Blob([typedArray], {type: 'application/octet-binary'});
$scope.url= URL.createObjectURL(blob);

然后在你看来

<a download="content.txt" ng-href="{{ url }}">download</a>

最后但同样重要的是,您应配置compileProvider以启用网址

app.config(['$compileProvider',
function ($compileProvider) {
    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|blob):/);
}]);