如何在不将其转换为字符串或 json 的情况下访问 Angular 2 http 响应主体?
How can I get access to the Angular 2 http response body without converting it to string or json?
我想将 REST 响应复制到 blob 中,但我无法执行某些操作,因为 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);
...
在这些方法得到实施之前,是否有我可以使用的解决方案?
在合并以下PR之前,我看不到其他解决方案:
虽然您有编译错误,但该字段可以在运行时使用...
因为我在 运行 遇到同样的问题时发现了这个问题(而且 Angular 的文档截至今天还没有更新)你现在可以使用:
let blob = new Blob([response.arrayBuffer()], { type: contentType });
如果您出于某种原因使用旧版本的 Angular 2,另一种解决方法是:
let blob = new Blob([(<any> response)._body], { type: contentType });
有一个更简单的解决方案可以将正文作为字符串访问,我在任何地方都没有看到它的记录:
let body = res.text()
设置requestoptions的responseType。这将使 response.blob() 方法起作用。
let headers = this.getAuthorizationHeader();
headers.append("Accept", "application/octet-stream");
return this.http
.get(url, new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob }))
.map((res: Response): Blob => {
return res.ok ? res.blob() : undefined;
});
@StudioLE 的插件。您可以使用 json() 方法将 return 数据作为 json.
let body = res.json()
我想将 REST 响应复制到 blob 中,但我无法执行某些操作,因为 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);
...
在这些方法得到实施之前,是否有我可以使用的解决方案?
在合并以下PR之前,我看不到其他解决方案:
虽然您有编译错误,但该字段可以在运行时使用...
因为我在 运行 遇到同样的问题时发现了这个问题(而且 Angular 的文档截至今天还没有更新)你现在可以使用:
let blob = new Blob([response.arrayBuffer()], { type: contentType });
如果您出于某种原因使用旧版本的 Angular 2,另一种解决方法是:
let blob = new Blob([(<any> response)._body], { type: contentType });
有一个更简单的解决方案可以将正文作为字符串访问,我在任何地方都没有看到它的记录:
let body = res.text()
设置requestoptions的responseType。这将使 response.blob() 方法起作用。
let headers = this.getAuthorizationHeader();
headers.append("Accept", "application/octet-stream");
return this.http
.get(url, new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob }))
.map((res: Response): Blob => {
return res.ok ? res.blob() : undefined;
});
@StudioLE 的插件。您可以使用 json() 方法将 return 数据作为 json.
let body = res.json()