我想复制一个REST响应成团块,但我不能做一些,因为blob()和arrayBuffer()尚未响应对象已经实施.Response Body是一个私有变量.
...
return this.http.get(url, {params: params, headers: headers})
.map(res => {
// can't access _body because it is private
// no method appears to exist to get to the _body without modification
new Blob([res._body], {type: res.headers.get('Content-Type')});
})
.catch(this.log);
...
Run Code Online (Sandbox Code Playgroud)
在实施这些方法之前,我是否可以使用解决方案?
我的问题非常类似于这个PDF Blob - 弹出窗口没有显示内容,但我使用的是Angular 2.问题的回答是将responseType设置为arrayBuffer,但它在Angular 2中不起作用,错误是reponseType没有存在于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);
});
}
Run Code Online (Sandbox Code Playgroud)
而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);
}
Run Code Online (Sandbox Code Playgroud)
我也尝试从FileSaver.js中保存方法,但是同样的问题,pdf打开,但内容不显示.谢谢