Javascript:在字节数组的新选项卡中打开 PDF [英] Javascript: Open PDF in new tab from byte array
问题描述
我使用带有 HTTP 资源的 AngularJS 来调用外部 API,我的响应是一个字节数组.我需要在新窗口中将此字节数组转换为 PDF.我还没有在这里看到任何非常好的解决方案,可以跨浏览器工作或纯 javascript.有没有办法做到这一点?
I'm using AngularJS with an HTTP resource to call an external API and my response is a byte array. I need to turn this byte array into a PDF in a new window. I haven't seen any very good solutions on here that work cross browser or that are pure javascript. Is there a way to do this?
这是我的代码:
Javascript
Document.preview({id: $scope.order.id}, function(data){
// Open PDF Here
var file = new Blob([data], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
});
推荐答案
您需要在服务调用中传递 responseType
You would need to pass the responseType in your service call
$http.post('/Service-URL', dataTO, {responseType: 'arraybuffer'});
然后在您的数据调用成功后,这应该会在新窗口中打开 pdf:-
then in the success of your data call this should open up pdf in a new window:-
getDocument()
.success(function(data) {
var file = new Blob([data], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
})
来自这个答案:- https://stackoverflow.com/a/21730535/3645957 by https://stackoverflow.com/users/2688545/michael
From this answer :- https://stackoverflow.com/a/21730535/3645957 by https://stackoverflow.com/users/2688545/michael
这篇关于Javascript:在字节数组的新选项卡中打开 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!