Вот пример того, как вы можете загрузить файл в angular, этот пример включает индикатор выполнения, если вам нужен
компонент. html
<input type="file" (change)="upload($event.target.files[0])">
<div class="progress" *ngIf="progress">
<div class="progress-bar" [style.width]="progress + '%'">{{progress}}%</div>
</div>
component.ts:
import { Component } from "@angular/core";
import {
HttpClient,
HttpEventType,
HttpErrorResponse
} from "@angular/common/http";
import { map, catchError } from "rxjs/operators";
import { throwError } from "rxjs";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
progress: number;
constructor(private http: HttpClient) {}
upload(file) {
this.progress = 1;
const formData = new FormData();
formData.append("file", file);
this.http
.post("yout-url-here", formData, {
reportProgress: true,
observe: "events"
})
.pipe(
map((event: any) => {
if (event.type == HttpEventType.UploadProgress) {
this.progress = Math.round((100 / event.total) * event.loaded);
} else if (event.type == HttpEventType.Response) {
this.progress = null;
}
}),
catchError((err: any) => {
this.progress = null;
alert(err.message);
return throwError(err.message);
})
)
.toPromise();
}
}
app.module.ts:
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { HttpClientModule } from "@angular/common/http";
import { AppComponent } from "./app.component";
@NgModule({
imports: [BrowserModule, FormsModule, HttpClientModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {}
Вот живой пример, проверьте это: https://stackblitz.com/edit/angular-upload-file-with-progress-bar