相关疑难解决方法(0)

如何在不将其转换为字符串或json的情况下访问Angular 2 http响应主体?

我想复制一个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)

在实施这些方法之前,我是否可以使用解决方案?

rest blob httpresponse angular2-http angular

23
推荐指数
3
解决办法
3万
查看次数

PDF Blob没有显示内容,Angular 2

我的问题非常类似于这个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打开,但内容不显示.谢谢

pdf blob bytearray angular

15
推荐指数
2
解决办法
4万
查看次数

标签 统计

angular ×2

blob ×2

angular2-http ×1

bytearray ×1

httpresponse ×1

pdf ×1

rest ×1