File

src/lib/matFileUpload/matFileUpload.component.ts

Description

A material design file upload component.

Implements

OnDestroy

Metadata

exportAs matFileUpload
host {
}
selector mat-file-upload
styleUrls ../matFileUploadQueue.scss
templateUrl ./matFileUpload.component.html

Index

Properties
Methods
Inputs
Outputs
Accessors

Constructor

constructor(HttpClient: HttpClient, matFileUploadQueue: MatFileUploadQueue)
Parameters :
Name Type Optional Description
HttpClient HttpClient
matFileUploadQueue MatFileUploadQueue

Inputs

file

Type: any

fileAlias

Type: string

Default value: "file"

httpRequestHeaders

Type: HttpHeaders | literal type

Default value: new HttpHeaders().set("Content-Type", "multipart/form-data")

httpRequestParams

Type: HttpParams | literal type

Default value: new HttpParams()

httpUrl

Type: string

Default value: 'http://localhost:8080'

id

Type: number

Outputs

onUpload $event type: EventEmitter
removeEvent

Output

$event type: EventEmitter

Methods

ngOnDestroy
ngOnDestroy()
Returns : void
Public remove
remove()
Returns : void
Public upload
upload()
Returns : void

Properties

Private _file
_file: any
Type : any
Private _id
_id: number
Type : number
Private fileUploadSubscription
fileUploadSubscription: any
Type : any
Private isUploading
isUploading: boolean
Type : boolean
Default value : false
Public loaded
loaded: number
Type : number
Default value : 0
Public matFileUploadQueue
matFileUploadQueue: MatFileUploadQueue
Type : MatFileUploadQueue
Decorators : Inject
Private progressPercentage
progressPercentage: number
Type : number
Default value : 0
Private total
total: number
Type : number
Default value : 0

Accessors

file
setfile(file: any)
id
getid()
setid(id: number)
import { Component, EventEmitter, Input, OnDestroy, OnInit, Output, Optional, Inject, forwardRef } from '@angular/core';
import { HttpClient, HttpEventType, HttpHeaders, HttpParams } from '@angular/common/http';
import { MatFileUploadQueue } from '../matFileUploadQueue/matFileUploadQueue.component';


/**
 * A material design file upload component.
 */
@Component({
    selector: 'mat-file-upload',
    templateUrl: `./matFileUpload.component.html`,
    exportAs: 'matFileUpload',
    host: {
      'class': 'mat-file-upload',
    },
    styleUrls: ['./../matFileUploadQueue.scss'],
  })
  export class MatFileUpload implements OnDestroy {

    constructor(
      private HttpClient: HttpClient
      ,@Inject(forwardRef(() => MatFileUploadQueue)) public matFileUploadQueue: MatFileUploadQueue
    ) {

        if(matFileUploadQueue) {
          this.httpUrl = matFileUploadQueue.httpUrl || this.httpUrl;
          this.httpRequestHeaders = matFileUploadQueue.httpRequestHeaders || this.httpRequestHeaders;
          this.httpRequestParams = matFileUploadQueue.httpRequestParams || this.httpRequestParams;
          this.fileAlias = matFileUploadQueue.fileAlias || this.fileAlias;
        }

    }

    private isUploading:boolean = false;



    /* Http request input bindings */
    @Input()
    httpUrl: string = 'http://localhost:8080';

    @Input()
    httpRequestHeaders: HttpHeaders | {
      [header: string]: string | string[];
    } = new HttpHeaders().set("Content-Type", "multipart/form-data");

    @Input()
    httpRequestParams: HttpParams | {
      [param: string]: string | string[];
    } = new HttpParams();

    @Input()
    fileAlias: string = "file";

    @Input()
    get file(): any {
      return this._file;
    }
    set file(file: any) {
      this._file = file;
      this.total = this._file.size;
    }

    @Input()
    set id(id: number) {
      this._id = id;
    }
    get id(): number {
      return this._id;
    }

    /** Output  */
    @Output() removeEvent = new EventEmitter<MatFileUpload>();
    @Output() onUpload = new EventEmitter();

    private progressPercentage: number = 0;
    public loaded: number = 0;
    private total: number = 0;
    private _file: any;
    private _id: number;
    private fileUploadSubscription: any;

    public upload(): void {
      this.isUploading = true;
      // How to set the alias?
      let formData = new FormData();
      formData.set(this.fileAlias, this._file, this._file.name);
      this.fileUploadSubscription = this.HttpClient.post(this.httpUrl, formData, {
       // headers: this.httpRequestHeaders,
        observe: "events",
        params: this.httpRequestParams,
        reportProgress: true,
        responseType: "json"
      }).subscribe((event: any) => {
        if (event.type === HttpEventType.UploadProgress) {
          this.progressPercentage = Math.floor( event.loaded * 100 / event.total );
          this.loaded = event.loaded;
          this.total = event.total;
        }
        this.onUpload.emit({ file: this._file, event: event });
      }, (error: any) => {
        if (this.fileUploadSubscription) {
          this.fileUploadSubscription.unsubscribe();
        }
        this.isUploading = false;
        this.onUpload.emit({ file: this._file, event: event });
      });
    }

    public remove(): void {
      if (this.fileUploadSubscription) {
        this.fileUploadSubscription.unsubscribe();
      }
      this.removeEvent.emit(this);
    }

    ngOnDestroy() {
      console.log('file '+ this._file.name + ' destryed...');
    }

}
<mat-card>
    <span class="file-info">{{file.name}}({{file.size | bytes}})</span>
    <section class="example-section">
        <mat-progress-bar class="example-margin" [value]="progressPercentage"></mat-progress-bar>
        <a [ngClass]="{'disabled' : isUploading}"><mat-icon class="action" (click)="upload()">file_upload</mat-icon></a>
        <mat-icon class="action" (click)="remove()">cancel</mat-icon>
    </section>
    <span class="file-info">{{progressPercentage}}%</span><span> {{loaded | bytes}} of {{total | bytes}}</span>
</mat-card>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""