我正在从API获取以下格式的Blob数据。
sum(rate(http_request_duration_sum))[24h] / sum(rate(http_request_duration_count))[24h]
我已经使用以下代码在IE,Chrome和Firefox中呈现PDF。
const data = 'JVBERi0xLjcNJeLjz9MKMzQgMCBvYmoNCjw8DQovVHlwZSAvQ2F0YWxvZw0KL1BhZ2VzIDcgMCBSDQovTWV0YWRhdGEgMjYgMCBSDQo+Pg0KZW5kb2JqDQozNSAwIG9iag0KPDwgL1R5cGUgL09ialN0bSAvTGVuZ3RoIDQxMCAvRmlyc3QgMTQ1IC9OIDIxIC9GaWx0ZXIgL0ZsYXRlRGVjb2RlID4+DQpzdHJlYW0NCnicxdTBqhoxFAbgvTDv8L/A7ck5SSYT..............';
以上代码可以正常工作并在Chrome和Firefox中打开PDF,但相同的代码在IE 11中不起作用,并显示访问被拒绝。在控制台中出现错误。
我尝试了下面的代码,但附加了错误
openPDF(pdfData: string){
const byteArray = new Uint8Array(atob(pdfData).split('').map(char => char.charCodeAt(0)));
const url = window.URL.createObjectURL(new Blob([byteArray], {type: 'application/pdf'}));
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.open(url);
}else{
window.open(url);
}
}
我也尝试过使用iFrame,它正在打开空白PDF。
我也尝试了下面的代码,该代码正在本地系统中下载PDF,但在打开文件时却显示与上面相同的错误。
let newBlob = new Blob([pdfData], {type: "application/pdf"});
window.navigator.msSaveOrOpenBlob(newBlob);
答案 0 :(得分:1)
const数据= 'JVBERi0xLjcNJeLjz9MKMzQgMCBvYmoNCjw8DQovVHlwZSAvQ2F0YWxvZw0KL1BhZ2VzIDcgMCBSDQovTWV0YWRhdGEgMjYgMCBSDQo + Pg0KZW5kb2JqDQozNSAwIG9iag0KPDwgL1R5cGUgL09ialN0bSAvTGVuZ3RoIDQxMCAvRmlyc3QgMTQ1IC9OIDIxIC9GaWx0ZXIgL0ZsYXRlRGVjb2RlID4 + DQpzdHJlYW0NCnicxdTBqhoxFAbgvTDv8L / A7ck5SSYT ..............';
从您的代码中看,数据似乎是base-64编码的字符串,在使用msSaveOrOpenBlob或msSaveBlob方法下载数据之前,我们需要解码base-64编码的字符串并将其转换为ByteArray。请参考以下代码(对我而言效果很好):
var data = "JVBERi0xLjQKJcOIw4HDhMOXDQo4IDAgb2JqCjw8Ci9GaWx0ZXIgL0ZsYXRlRGVjb2RlIAovTGVuZ3RoIDEyMSAKPj4gc3RyZWFtCnicVYuxCsIwFEX3fsUZ06HxvdrktasgiJuQTdxMdYgUM+jvWxdBLtx77nB4In6M9tf1hn1JsXUnCwxh64NGOhPxqgM1M3Nq2CU2h1xeaE+aUWSN/vQo3mQiPc7unktZWrreRty7RcUtdf0aXLm2F9KxgX1qPvc2IBcKZW5kc3RyZWFtCmVuZG9iago2IDAgb2JqCjw8Ci9Hcm91cCA8PAovUyAvVHJhbnNwYXJlbmN5IC9LIHRydWUgL0kgdHJ1ZSAvQ1MgNCAwIFIgID4+ICAKL0NvbnRlbnRzIDggMCBSICAKL1Jlc291cmNlcyA8PAovRm9udCA8PAovSGVsdiA5IDAgUiAgPj4gIC9Db2xvclNwYWNlIDw8Ci9EZWZhdWx0UkdCIDQgMCBSICA+PiAgL1Byb2NTZXQgWy9QREYgL1RleHRdID4+ICAKL1R5cGUgL1BhZ2UgCi9QYXJlbnQgNyAwIFIgIAovTWVkaWFCb3ggWzAgMCA1OTUuMjM4IDg0MS44MzZdIAo+PiBlbmRvYmoKNyAwIG9iago8PAovS2lkcyBbNiAwIFJdIAovVHlwZSAvUGFnZXMgCi9Db3VudCAxIAovUGFyZW50IDIgMCBSICAKPj4gZW5kb2JqCjIgMCBvYmoKPDwKL0tpZHMgWzcgMCBSXSAKL1R5cGUgL1BhZ2VzIAovQ291bnQgMSAKPj4gZW5kb2JqCjEgMCBvYmoKPDwKL091dGxpbmVzIDMgMCBSICAKL1BhZ2VzIDIgMCBSICAKL1R5cGUgL0NhdGFsb2cgCi9QYWdlTW9kZSAvVXNlTm9uZSAKPj4gZW5kb2JqCjQgMCBvYmoKWy9JQ0NC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L1NpemUgMTEgCi9JbmZvIDEwIDAgUiAgCi9Sb290IDEgMCBSIAo+PgpzdGFydHhyZWYKMzY5OAolJUVPRgo=";
var fileName = "testpdf.pdf";
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
var byteCharacters = window.atob(data);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
var blob = new Blob([byteArray], { type: 'application/pdf' });
window.navigator.msSaveOrOpenBlob(blob, fileName);
}
此外,当您选择用于存储pdf文件的文件夹时,请检查文件夹属性并确保您具有“写入”和“读取”权限。