繁体   English   中英

显示API返回的PDF文档

[英]Display a PDF document returned by API

我的API返回了一个需要我的用户进行身份验证的PDF文档。 在我的AngularJS应用程序上,用户单击一个调用函数的链接:

 <a ng-click="getBankDetails()">Print bank details</a>

控制器:

$scope.getBankDetails = ->
$http.get(ENV.apiEndpoint + "/api/v1/users/get_bank_details", { headers: { 'Accept': 'application/pdf' }, responseType: 'arraybuffer'  })
   .success((resp) ->
     file = new Blob([resp], {type: 'application/pdf'})
     fileURL = URL.createObjectURL(file)
     window.open(file);
   )

但这不起作用。

我尝试删除API上的身份验证需求。 上面的方法仍然不起作用,但是如果我这样做

window.open(ENV.apiEndpoint + "/api/v1/users/get_bank_details")

这样可行。 除了确认我的API可以正常工作之外,它并没有真正的帮助。

我正在使用ng-token-auth进行身份验证

它存储在cookie中,因为我在这里不使用cordova

$authProvider.configure
  apiUrl: ENV.apiEndpoint
  storage: if _.isUndefined(window.cordova) then "cookies" else "localStorage"

任何帮助将不胜感激

如果您使用的是基于令牌的身份验证,则可以通过将authToken添加到URL中以接收文件来做到这一点。 根据该请求,通过后端将authToken作为GET-Param处理:

$scope.getBankDetails = function () {
 window.open(ENV.apiEndpoint + "/api/v1/users/get_bank_details?authToken=XYZ");
}

暂无
暂无

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

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