【问题标题】:Angular $http $resource equivalent for PDF GET requestPDF GET 请求的 Angular $http $resource 等效项
【发布时间】: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


    【解决方案1】:

    为什么不直接使用带有 ng-href 的普通锚标记

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

    【讨论】:

      猜你喜欢
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 2018-11-13
      相关资源
      最近更新 更多