Angular HTTP Progress Tracking
raw JSON →angular-progress-http is a specialized library providing upload and download progress tracking for HTTP requests within Angular applications. Its `1.0.0` version, which is the latest, is explicitly designed for Angular 4 and 5 environments, relying on the `@angular/http` module and RxJS 5. The library functions as a thin wrapper around Angular's standard `Http` service, extending its capabilities to expose XMLHttpRequest-level progress events through an Angular-friendly observable pattern. Key differentiators include its fluent, immutable API, where `withUploadProgressListener` and `withDownloadProgressListener` methods return new service instances, ensuring that progress handlers are not accidentally overwritten and providing type-safe chaining. This design allows for separate, distinct listeners for both upload and download events, a crucial feature for applications involving large file transfers or data intensive operations during its active development period. Given the deprecation and subsequent removal of `@angular/http` in newer Angular versions, this package is no longer compatible with modern Angular applications.
Common errors
error Error: StaticInjectorError(AppModule)[ProgressHttp]: NullInjectorError: No provider for ProgressHttp! ↓
ProgressHttpModule is listed in the imports array of your main Angular module (e.g., AppModule). Also, ensure HttpModule (from @angular/http) is imported first, as ProgressHttpModule relies on it. error Property 'withUploadProgressListener' does not exist on type 'Http'. ↓
private http: Http to private http: ProgressHttp in your component or service. error TypeError: Cannot read property 'subscribe' of undefined ↓
post, get, or other HTTP method call is syntactically correct and that HttpModule from @angular/http is imported in your root module before ProgressHttpModule. Warnings
breaking This library is built for Angular 4 and 5, relying on the `@angular/http` module. `@angular/http` was deprecated in Angular 5 and completely removed in Angular 9. This package is fundamentally incompatible with Angular versions 9 and newer. ↓
breaking Older Angular 2 projects require a specific version (`0.5.1`) of `angular-progress-http`. The `1.0.0` version is only compatible with Angular 4 and 5. ↓
gotcha The `withUploadProgressListener` and `withDownloadProgressListener` methods are immutable; they return new instances of the service. Chaining multiple calls of the *same* listener type (`.withUploadProgressListener(...).withUploadProgressListener(...)`) will overwrite previous listeners, only the last one will be active. The API design enforces this for type safety. ↓
Install
npm install angular-progress-http yarn add angular-progress-http pnpm add angular-progress-http Imports
- ProgressHttpModule wrong
const ProgressHttpModule = require('angular-progress-http');correctimport { ProgressHttpModule } from 'angular-progress-http'; - ProgressHttp wrong
import ProgressHttp from 'angular-progress-http';correctimport { ProgressHttp } from 'angular-progress-http'; - Progress
import { Progress } from 'angular-progress-http';
Quickstart
import { Component, NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { ProgressHttpModule, ProgressHttp } from 'angular-progress-http';
import { BrowserModule } from '@angular/platform-browser';
@Component({
selector: 'app-root',
template: `
<h1>HTTP Progress Demo</h1>
<button (click)="uploadFile()">Upload Dummy File</button>
<p>Upload Progress: {{ uploadProgress }}%</p>
<p>Download Progress: {{ downloadProgress }}%</p>
`
})
export class AppComponent {
uploadProgress: number = 0;
downloadProgress: number = 0;
constructor(private http: ProgressHttp) {}
uploadFile() {
const formData = new FormData();
// Simulate a file or some data
formData.append('data', 'some long string content to simulate a file for upload progress testing.');
this.http
.withUploadProgressListener(progress => {
console.log(`Uploading ${progress.percentage}%`);
this.uploadProgress = progress.percentage;
})
.withDownloadProgressListener(progress => {
console.log(`Downloading ${progress.percentage}%`);
this.downloadProgress = progress.percentage;
})
.post('/api/upload', formData) // Replace with a real backend endpoint for testing
.subscribe(
(response) => {
console.log('Upload complete!', response);
this.uploadProgress = 100;
this.downloadProgress = 100;
},
(error) => {
console.error('Upload failed!', error);
this.uploadProgress = 0;
this.downloadProgress = 0;
}
);
}
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpModule, // Required for ProgressHttpModule
ProgressHttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }