我的Angular项目使用$resource来处理我对Web API的所有请求,但我想知道如何处理PDF请求中的数据,在这里我发现了一个使用$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构建页面上使用的所有请求时:
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抱怨[$se:itype]试图信任需要字符串的内容中的非字符串值:Context:resourceUrl
任何建议都将不胜感激
为什么不在ng href 中使用普通的锚标记
https://docs.angularjs.org/api/ng/directive/ngHref