我试图使用角8在另一个浏览器选项卡中打开PDF。Spring引导API作为字节数组返回PDF:
@GetMapping(value = "/pdf")
public ResponseEntity<byte[]> beve(HttpServletRequest request) {
return new ResponseEntity<byte[]>(service.getPdf(request), HttpStatus.OK);
}在角度上,为服务:
getPdf(): any {
const httpOptions = {
'responseType' : 'arraybuffer' as 'json'
};
return this.http.get<any>(this.url + "/pdf", httpOptions);
}以及调用它的组件:
getPdf() {
this.service.getPdf().subscribe((response => {
const file = new Blob([response], { type: 'application/pdf' });
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(file, "app.pdf");
} else {
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
}
}));
}但是,当PDF在一个新的选项卡中打开时,我会得到以下错误:

我做错了什么?
谢谢
发布于 2022-06-05 13:58:19
我也遇到过类似的情况。尝试将数组缓冲区转换为base64字符串,它在所有浏览器中都能正常工作。
var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(data.data)));
var pdfAsDataUri = "data:application/pdf;base64," + base64String;
windoe.open(pdfAsDataUri);https://stackoverflow.com/questions/62042843
复制相似问题