简体   繁体   English

等效于GET请求的Angular $ http $ resource

[英]Angular $http $resource equivalent for PDF GET request

My Angular project use $resource for all the request I make to the Web API, but I was looking to figure out how to handle the data from a request for PDF, looking on here I found a snippet that works perfectly using $http, I am being trying to get the same result from $resource but not success there: 我的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);
    });

Works perfectly using the $sce service to validate the URL to the $scope.content I use in the pop window. 使用$ sce服务完美验证我在弹出窗口中使用的$ scope.content的URL可以正常工作。 The problem is when I use $resource build on the service for all the request I use on the page: 问题是,当我对页面上使用的所有请求都在服务上使用$ 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
                };
            }
        })

then I called form the controller 然后我打电话给控制器

            SAJAPdata.InvoicePDF.get().$promise.then(function(pdf) {

            $scope.content = $sce.trustAsResourceUrl(pdf);

        });

but not success Angular complains about [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: resourceUrl 但没有成功Angular抱怨[$ sce:itype]尝试信任需要字符串的内容中的非字符串值:Context:resourceUrl

Any suggestion will be appreciated 任何建议将不胜感激

Why not just use a normal anchor tag with ng-href 为什么不将普通锚标签与ng-href一起使用

https://docs.angularjs.org/api/ng/directive/ngHref https://docs.angularjs.org/api/ng/directive/ngHref

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM