我需要将 PDF 文档嵌入到 html 中,但该文档需要作为 header 传入的 token 身份验证。
headers: {'X-Authentication': t}
其中 t
是我在服务器身份验证后检索到的 token 。
如何在客户端获取文档并将其显示在 iframe 中?
我正在使用 angular 对服务器进行 REST 调用。
最佳答案
我不完全确定这会奏效,但这是我的处理方式...
假设您可以在您的应用程序中获取 PDF 二进制文件,请这样做
$http.get('/path/to/pdf', {
responseType: 'blob',
headers: {
'X-Authentication': t
},
transformResponse: function(data) {
// don't forget to inject $window
return ($window.URL || $window.webkitURL).createObjectURL(data);
}
})
这将返回一个 $http
promise(就像一个普通的 promise,但有 success
和 error
方法),其中数据被解析为您可以在 iframe 的 src
属性中使用的 URI。例如……
.success(function(uri) {
$scope.iframeUri = uri;
});
参见 https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
关于javascript - 使用 header 身份验证 token 通过 REST API 获取 PDF 并在 iframe 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31689420/