【发布时间】:2016-03-25 10:21:22
【问题描述】:
我的 Angular 项目对我向 Web API 发出的所有请求都使用了 $resource,但我想弄清楚如何处理来自 PDF 请求的数据,在这里我发现了一个运行良好的 sn-p使用 $http,我试图从 $resource 获得相同的结果,但在那里没有成功:
$http.get('/api/pdf/shipper',
{responseType:'arraybuffer'})
.success(function (response) {
var file = new Blob([(response)], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
$scope.content = $sce.trustAsResourceUrl(fileURL);
});
完美地使用 $sce 服务来验证我在弹出窗口中使用的 $scope.content 的 URL。 问题是当我在服务上使用 $resource build 来处理我在页面上使用的所有请求时:
InvoicePDF: $resource('/api/pdf/invoice', {}, {
method: 'GET',
headers: {
accept: 'application/pdf'
},
responseType: 'arraybuffer',
cache: true,
transformResponse: function (data) {
var pdf;
if (data) {
pdf = new Blob([data], {
type: 'application/pdf'
});
}
return {
response: pdf
};
}
})
然后我从控制器调用
SAJAPdata.InvoicePDF.get().$promise.then(function(pdf) {
$scope.content = $sce.trustAsResourceUrl(pdf);
});
但没有成功 Angular 抱怨 [$sce:itype] 试图信任需要字符串的内容中的非字符串值:上下文:resourceUrl
任何建议将不胜感激
【问题讨论】:
标签: javascript angularjs