Skip to content

Commit

Permalink
Angular Forms In Depth
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jan 7, 2021
1 parent 00d9b65 commit bfa550a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/app/file-upload/file-upload.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@
</button>

</div>

<mat-progress-bar class="progress-bar" mode="determinate"
*ngIf="uploadProgress"
[value]="uploadProgress">

</mat-progress-bar>
31 changes: 21 additions & 10 deletions src/app/file-upload/file-upload.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {Component, Input} from '@angular/core';
import {HttpClient, HttpEventType} from '@angular/common/http';
import {catchError, finalize} from 'rxjs/operators';
import {AbstractControl, ControlValueAccessor, NG_VALIDATORS, NG_VALUE_ACCESSOR, Validator} from '@angular/forms';
import {noop, of} from 'rxjs';
import {of} from 'rxjs';


@Component({
Expand All @@ -19,6 +18,8 @@ export class FileUploadComponent {

fileUploadError = false;

uploadProgress:number;

constructor(private http: HttpClient) {

}
Expand All @@ -37,14 +38,24 @@ export class FileUploadComponent {

this.fileUploadError = false;

this.http.post("/api/thumbnail-upload", formData)
.pipe(
catchError(error => {
this.fileUploadError = true;
return of(error);
})
)
.subscribe();
this.http.post("/api/thumbnail-upload", formData, {
reportProgress: true,
observe: 'events'
})
.pipe(
catchError(error => {
this.fileUploadError = true;
return of(error);
}),
finalize(() => {
this.uploadProgress = null;
})
)
.subscribe(event => {
if (event.type == HttpEventType.UploadProgress) {
this.uploadProgress = Math.round(100 * (event.loaded / event.total));
}
});



Expand Down

0 comments on commit bfa550a

Please sign in to comment.