Angular HTTP Progress Tracking

1.0.0 · abandoned · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates how to import `ProgressHttpModule` into an Angular module, inject `ProgressHttp` into a component, and utilize its `withUploadProgressListener` and `withDownloadProgressListener` methods to track and display HTTP request progress.

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 { }

view raw JSON →