我遇到了与此问题非常相似的困扰:PDF Blob - Pop up window not showing content,但我使用的是 Angular 2。该问题的回答是将 responseType 设置为 arrayBuffer,但在 Angular 2 中却不起作用,错误提示是 responseType 不存在于 RequestOptionsArgs 类型中。我还尝试通过 BrowserXhr 进行扩展,但仍然不起作用(https://github.com/angular/http/issues/83)。
我的代码如下:
createPDF(customerServiceId: string) {
console.log("Sending GET on " + this.getPDFUrl + "/" + customerServiceId);
this._http.get(this.getPDFUrl + '/' + customerServiceId).subscribe(
(data) => {
this.handleResponse(data);
});
}
而且 handleResponse 方法:
handleResponse(data: any) {
console.log("[Receipt service] GET PDF byte array " + JSON.stringify(data));
var file = new Blob([data._body], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
}
我也尝试了FileSaver.js的saveAs方法,但是遇到了同样的问题,PDF文件可以打开,但内容无法显示。谢谢