/ngx-uploadx

Angular Resumable Upload Module

Primary LanguageTypeScriptMIT LicenseMIT

ngx-uploadx

Angular Resumable Upload Module

npm version Build status

Key Features

  • Pause / Resume / Cancel Uploads
  • Retries using exponential back-off strategy
  • Chunking

Setups

  • Add ngx-uploadx module as dependency :
  npm install ngx-uploadx
  • Import UploadxModule in the root module:
//...
import { UploadxModule } from 'ngx-uploadx';

@NgModule({
  imports: [
    UploadxModule,
   // ...
});

Basic usage

// Component code
//...
import { Observable } from 'rxjs';
import { UploadxOptions, UploadState } from 'ngx-uploadx';

@Component({
  selector: 'app-home',
  templateUrl: `
  <input type="file" [uploadx]="options" (uploadxState)="onUpload($event)">
  `
})

export class AppHomeComponent {
  options: UploadxOptions = { url: `[URL]`};
  onUpload(state: Observable<UploadState>) {
    state
      .subscribe((item: UploadState) => {
         console.log(item);
         //...
      }
 }

Server-side setup

Directive

<input
  type="file"
  [uploadx]="options"
  [uploadxAction]="control"
  (uploadxState)="onUpload($event)"
/>

Selector

[uploadx]

inputs

  • [uploadx]: UploadxOptions
  • [uploadxAction]: UploadxControlEvent

Output

  • (uploadxState): $event <Observable>UploadState

Service

UploadxService

Public Methods

  • init(options: UploadxOptions): Observable<UploadState>

    Set global module options

    // example:
    uploadxOptions: UploadxOptions = {
      concurrency: 2,
      allowedTypes: 'image/*,video/*',
      endpoint: `${environment.api}/upload?uploadType=uploadx`,
      token:  () => {
        return localStorage.getItem('access_token');
      },
      autoUpload: false,
      chunkSize: 1024 * 256 * 8
    };
    ngOnInit() {
      this.uploadService.init(this.uploadxOptions)
        .subscribe((item: UploadState) => {
          console.log(item);
          //...
        }
    }
  • handleFileList(fileList: FileList): void

    Add files to the upload queue

  • control(event: UploadxControlEvent): void

    Send event

    // example:
    
    pause(uploadId: string) {
      this.uploadService.control({ action: 'pause', uploadId });
    }

Interfaces

UploadxOptions

Name Defaults? Description
[allowedTypes] - Set "accept" attribute
[autoUpload] true Auto upload with global options
[chunkSize] 0 If set to > 0 use chunked upload
[concurrency] 2 Limit the number of concurrent uploads
[headers] - Custom headers
[method] "POST" Upload API initial method
[token] - _Auth Bearer token _
[endpoint] "/upload/" API URL
[url] "/upload/" (alias) API URL
[withCredentials] false Use withCredentials xhr option

<Observable> UploadState

Name Type Description
file File FileAPI File
name string file name
progress number progress percentage
percentage number (alias) progress percentage
remaining number ETA
response any success/error response
size number file size
speed number upload speed bytes/sec
status UploadStatus 'added', 'queue', 'uploading', 'complete', 'error', 'cancelled', 'paused'
uploadId string internal upload id
URI string file URI

UploadItem

Item custom options, override globals

Name Type Description
[method] string API initial method
[uploadId] string internal upload id ( ReadOnly )
[URI] string Item URL
[headers] { [key: string]: string } Add custom headers
[metadata] any Add custom data

UploadxControlEvent

Name Type Description
action UploadAction 'uploadAll', 'upload', 'cancel', 'cancelAll', 'pauseAll, 'pause', 'refreshToken'
[uploadId] string internal upload id ( ReadOnly )
[itemOptions] UploadItem Item custom options

Run demo

  • Start server npm run server
  • Run demo app npm start
  • Navigate to http://localhost:4200/

Build

Run npm run build to build the lib.

packaged by ng-packagr

Contributing

Pull requests are welcome!

Links

License

The MIT License (see the LICENSE file for the full text)