Newer
Older
import { Component, EventEmitter, Input, OnInit, Output, HostBinding } from '@angular/core';
import { HttpClient, HttpEventType, HttpHeaders, HttpParams,
HttpErrorResponse, HttpEvent } from '@angular/common/http';
@Component({

andreistroescu
committed
selector: 'iqb-files-upload',
templateUrl: `./iqbFilesUpload.component.html`,
exportAs: 'iqbFilesUpload',
styleUrls: ['./../iqb-files.scss'],

andreistroescu
committed
export class IqbFilesUploadComponent implements OnInit {
@HostBinding('class') myclass = 'iqb-files-upload';
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
constructor(
private myHttpClient: HttpClient) { }
// ''''''''''''''''''''''''
private _status: UploadStatus;
get status(): UploadStatus {
return this._status;
}
set status(newstatus: UploadStatus) {
this._status = newstatus;
this.statusChangedEvent.emit(this);
}
// ''''''''''''''''''''''''
private requestResponseText: string;
get statustext(): string {
let myreturn = '';
switch (this._status) {
case UploadStatus.busy: {
myreturn = 'Bitte warten';
break;
}
case UploadStatus.ready: {
myreturn = 'Bereit';
break;
}
default: {
myreturn = this.requestResponseText;
break;
}
}
return myreturn;
}
/* Http request input bindings */
@Input()
httpUrl = '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 = 'file';
@Input()
tokenName = '';
@Input()
token = '';
@Input()
folderName = '';
@Input()
folder = '';
@Input()
get file(): any {
return this._file;
}
set file(file: any) {
this._file = file;
this._filedate = this._file.lastModified;
this.total = this._file.size;
}
@Input()
set id(id: number) {
this._id = id;
}
get id(): number {
return this._id;
}

andreistroescu
committed
@Output() removeFileRequestEvent = new EventEmitter<IqbFilesUploadComponent>();
@Output() statusChangedEvent = new EventEmitter<IqbFilesUploadComponent>();
private progressPercentage = 0;
public loaded = 0;
private total = 0;
private _file: any;
private _filedate = '';
private _id: number;
private fileUploadSubscription: any;
ngOnInit() {
this._status = UploadStatus.ready;
this.requestResponseText = '';

andreistroescu
committed
this.upload();
}
// ==================================================================

andreistroescu
committed
private upload(): void {
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
if (this.status === UploadStatus.ready) {
this.status = UploadStatus.busy;
const formData = new FormData();
formData.set(this.fileAlias, this._file, this._file.name);
if ((typeof this.tokenName !== 'undefined') && (typeof this.token !== 'undefined')) {
if (this.tokenName.length > 0) {
formData.append(this.tokenName, this.token);
}
}
if ((typeof this.folderName !== 'undefined') && (typeof this.folder !== 'undefined')) {
if (this.folderName.length > 0) {
formData.append(this.folderName, this.folder);
}
}
this.fileUploadSubscription = this.myHttpClient.post(this.httpUrl, formData, {
// headers: this.httpRequestHeaders,
observe: 'events',
params: this.httpRequestParams,
reportProgress: true,
responseType: 'json'
}).subscribe((event: HttpEvent<any>) => {
if (event.type === HttpEventType.UploadProgress) {
this.progressPercentage = Math.floor( event.loaded * 100 / event.total );
this.loaded = event.loaded;
this.total = event.total;
this.status = UploadStatus.busy;
} else if (event.type === HttpEventType.Response) {
this.requestResponseText = event.body;
if ((this.requestResponseText.length > 5) && (this.requestResponseText.substr(0, 2) === 'e:')) {
this.requestResponseText = this.requestResponseText.substr(2);
this.status = UploadStatus.error;
} else {
this.status = UploadStatus.ok;

andreistroescu
committed
this.remove();
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
}
}
}, (errorObj: HttpErrorResponse) => {
if (this.fileUploadSubscription) {
this.fileUploadSubscription.unsubscribe();
}
this.status = UploadStatus.error;
if (errorObj.status === 401) {
this.requestResponseText = 'Fehler: Zugriff verweigert - bitte (neu) anmelden!';
} else if (errorObj.status === 503) {
this.requestResponseText = 'Fehler: Server meldet Problem mit Datenbank oder Datei zu groß.';
} else if (errorObj.error instanceof ErrorEvent) {
this.requestResponseText = 'Fehler: ' + (<ErrorEvent>errorObj.error).message;
} else {
this.requestResponseText = 'Fehler: ' + errorObj.message;
}
});
}
}
// ==================================================================
public remove(): void {
if (this.fileUploadSubscription) {
this.fileUploadSubscription.unsubscribe();
}
this.removeFileRequestEvent.emit(this);
}
}
export enum UploadStatus {
ready,
busy,
ok,
error
}