src/lib/matFileUpload/matFileUpload.component.ts
A material design file upload component.
exportAs | matFileUpload |
host | { |
selector | mat-file-upload |
styleUrls | ../matFileUploadQueue.scss |
templateUrl | ./matFileUpload.component.html |
Properties |
|
Methods |
|
Inputs |
Outputs |
Accessors |
constructor(HttpClient: HttpClient, matFileUploadQueue: MatFileUploadQueue)
|
||||||||||||
Parameters :
|
file
|
Type: |
fileAlias
|
Type:
Default value: |
httpRequestHeaders
|
Type:
Default value: |
httpRequestParams
|
Type:
Default value: |
httpUrl
|
Type:
Default value: |
id
|
Type: |
onUpload
|
$event type: EventEmitter
|
removeEvent
|
Output $event type: EventEmitter
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
Public remove |
remove()
|
Returns :
void
|
Public upload |
upload()
|
Returns :
void
|
Private _file |
_file:
|
Type : any
|
Private _id |
_id:
|
Type : number
|
Private fileUploadSubscription |
fileUploadSubscription:
|
Type : any
|
Private isUploading |
isUploading:
|
Type : boolean
|
Default value : false
|
Public loaded |
loaded:
|
Type : number
|
Default value : 0
|
Public matFileUploadQueue |
matFileUploadQueue:
|
Type : MatFileUploadQueue
|
Decorators : Inject
|
Private progressPercentage |
progressPercentage:
|
Type : number
|
Default value : 0
|
Private total |
total:
|
Type : number
|
Default value : 0
|
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>