kar*_*aja 5 httpclient angular angular7
我正在使用HTTP
POST
方法下载文件。我想调用另一种方法来向最终用户显示下载进度,直到文件下载完成。如何使用reportProgress
在HttpClient
此。
downfile(file: any): Observable<any> {
return this.http.post(this.url , app, {
responseType: "blob", reportProgress: true, headers: new HttpHeaders(
{ 'Content-Type': 'application/json' },
)
});
}
Run Code Online (Sandbox Code Playgroud)
Sud*_*nda 13
您需要使用reportProgress: true
来显示任何HTTP
请求的进度。如果要查看,all events, including the progress of transfers
还需要使用observe: 'events'
option并返回Observable
type HttpEvent
。然后,您可以捕获events(DownloadProgress, Response..etc)
component方法中的所有内容。
在Angular官方文档中找到更多详细信息。
downfile(file: any): Observable<HttpEvent<any>>{
return this.http.post(this.url , app, {
responseType: "blob", reportProgress: true, observe: "events", headers: new HttpHeaders(
{ 'Content-Type': 'application/json' },
)
});
}
Run Code Online (Sandbox Code Playgroud)
然后在组件中,您可以捕获所有events
如下内容。
this.myService.downfile(file)
.subscribe(event => {
if (event.type === HttpEventType.DownloadProgress) {
console.log("download progress");
}
if (event.type === HttpEventType.Response) {
console.log("donwload completed");
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3587 次 |
最近记录: |