Javascript AngularJS:$http.get方法下载pdf文件 我正在SPA中使用$http.get(…)方法来获取pdf下载 在SPA中,打印方法给出空白PDF。 但当我进行调试时,数据来自API

Javascript AngularJS:$http.get方法下载pdf文件 我正在SPA中使用$http.get(…)方法来获取pdf下载 在SPA中,打印方法给出空白PDF。 但当我进行调试时,数据来自API,javascript,angularjs,pdf,Javascript,Angularjs,Pdf,你能帮忙吗 这是以application/pdf形式响应流的API实现 public Stream GetConsumerInformationReport(Guid id) { .......... var stream = cryRpt.ExportToStream(ExportFormatType.PortableDocFormat); return stream; } 从API获取数据的SPA实现 var print = function () {

你能帮忙吗

这是以application/pdf形式响应流的API实现

public Stream GetConsumerInformationReport(Guid id)
 {
   ..........
   var stream = cryRpt.ExportToStream(ExportFormatType.PortableDocFormat);
   return stream;
}
从API获取数据的SPA实现

var print = function () {
            var downloadPath = apiEndPoint + 'Reports/' + $state.current.data.printPrefix + '.pdf';
            $http.get(downloadPath,httpConfig).
                success(function (data) {
                    var blob = new Blob([data], { type: "application/pdf" });
                    var objectUrl = URL.createObjectURL(blob);
                    $window.open(objectUrl);
            }).
            error(function (data, status, headers, config) {
            // if there's an error you should see it here
            });

        };

从这里使用FileSaver.js

然后像这样定义下载方法。仅将其作为灵感,不要复制粘贴运行:) 原始版本可在此处找到-

//在ng控制器中定义方法下载()
$scope.download=()=>{
//表示正在下载
$scope.isDownloading=true;
返回$http.get(下载路径,httpConfig)。$promise.then((数据:any)=>{
//使用saveAs.js(即将推出的HTML5API的一部分,但到目前为止是polyfill)
var blob=data.response.blob;
var fileName:string=data.response.fileName | |“document.pdf”;
//SaveAs可从SaveAs.js获得http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js
($window).saveAs(blob,文件名);
})
.最后(()=>{
$scope.isDownloading=false;
});
}

您是否尝试在控制台中返回成功错误数据?在控制台中-没有错误,但它是进入成功内部的,并且数据在那里
    //Define method download() in your ng controller

    $scope.download = () => {
          //Indicates that download is in progress
          $scope.isDownloading = true;

          return $http.get(downloadPath,httpConfig).$promise.then((data: any) => {
                  //using saveAs.js (part of upcoming HTML5 API, but so far a polyfill)
                  var blob = data.response.blob;

                  var fileName: string = data.response.fileName || 'document.pdf';

                  //SaveAs is available at saveAs.js from http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js
                  (<any>$window).saveAs(blob, fileName);
              })
              .finally(() => {
                 $scope.isDownloading = false;
          });
      }