发布数据后获取 Blob 对象
Getting Blob object after posting data
我正在向数据发布一些休息 api 并且应该得到一个 blob 对象作为响应。我需要下载那个 blob,或者我需要在我的 angular 应用程序中邮寄它。但我想弄清楚如何捕获 blob 对象作为响应。我的数据服务文件是:
export class DataService<Type> {
private resolveSuffix: string = '?resolve=true';
private actionUrl: string;
private headers: Headers;
constructor(private http: Http, private httpClient:HttpClient) { }
public issueId(asset: Type): Observable<Blob> {
return this.httpC.post('http://localhost:3001/api/system/identities/issue', asset, {responseType: "blob"})
.toPromise();
}
我遇到错误:
property httpC doesn't exist on type 'DataService<Type>'
编辑:第一个错误已修复,现在错误是
类型 'Promise' 不可分配给类型 'Observable'
看看你的构造函数。您在签名中称它为 httpC
而不是 httpClient
,这意味着您的调用应改用 this.httpC.post
。
now the error is Type 'Promise' is not assignable to type 'Observable'
错误似乎很明显。如果您查看 httpClient 方法的文档,您可以看到它们 return Observable.
您的服务的 return 值是 Observable
您只需要从 return 语句中删除 toPromise()
。
我正在向数据发布一些休息 api 并且应该得到一个 blob 对象作为响应。我需要下载那个 blob,或者我需要在我的 angular 应用程序中邮寄它。但我想弄清楚如何捕获 blob 对象作为响应。我的数据服务文件是:
export class DataService<Type> {
private resolveSuffix: string = '?resolve=true';
private actionUrl: string;
private headers: Headers;
constructor(private http: Http, private httpClient:HttpClient) { }
public issueId(asset: Type): Observable<Blob> {
return this.httpC.post('http://localhost:3001/api/system/identities/issue', asset, {responseType: "blob"})
.toPromise();
}
我遇到错误:
property httpC doesn't exist on type 'DataService<Type>'
编辑:第一个错误已修复,现在错误是 类型 'Promise' 不可分配给类型 'Observable'
看看你的构造函数。您在签名中称它为 httpC
而不是 httpClient
,这意味着您的调用应改用 this.httpC.post
。
now the error is Type 'Promise' is not assignable to type 'Observable'
错误似乎很明显。如果您查看 httpClient 方法的文档,您可以看到它们 return Observable.
您的服务的 return 值是 Observable
您只需要从 return 语句中删除 toPromise()
。