Skip to content

Commit bfa550a

Browse files
author
Your Name
committed
Angular Forms In Depth
1 parent 00d9b65 commit bfa550a

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/app/file-upload/file-upload.component.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@
1717
</button>
1818

1919
</div>
20+
21+
<mat-progress-bar class="progress-bar" mode="determinate"
22+
*ngIf="uploadProgress"
23+
[value]="uploadProgress">
24+
25+
</mat-progress-bar>

src/app/file-upload/file-upload.component.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {Component, Input} from '@angular/core';
22
import {HttpClient, HttpEventType} from '@angular/common/http';
33
import {catchError, finalize} from 'rxjs/operators';
4-
import {AbstractControl, ControlValueAccessor, NG_VALIDATORS, NG_VALUE_ACCESSOR, Validator} from '@angular/forms';
5-
import {noop, of} from 'rxjs';
4+
import {of} from 'rxjs';
65

76

87
@Component({
@@ -19,6 +18,8 @@ export class FileUploadComponent {
1918

2019
fileUploadError = false;
2120

21+
uploadProgress:number;
22+
2223
constructor(private http: HttpClient) {
2324

2425
}
@@ -37,14 +38,24 @@ export class FileUploadComponent {
3738

3839
this.fileUploadError = false;
3940

40-
this.http.post("/api/thumbnail-upload", formData)
41-
.pipe(
42-
catchError(error => {
43-
this.fileUploadError = true;
44-
return of(error);
45-
})
46-
)
47-
.subscribe();
41+
this.http.post("/api/thumbnail-upload", formData, {
42+
reportProgress: true,
43+
observe: 'events'
44+
})
45+
.pipe(
46+
catchError(error => {
47+
this.fileUploadError = true;
48+
return of(error);
49+
}),
50+
finalize(() => {
51+
this.uploadProgress = null;
52+
})
53+
)
54+
.subscribe(event => {
55+
if (event.type == HttpEventType.UploadProgress) {
56+
this.uploadProgress = Math.round(100 * (event.loaded / event.total));
57+
}
58+
});
4859

4960

5061

0 commit comments

Comments
 (0)