1 | import {Component, Input} from "@angular/core";
|
2 |
|
3 | import {FileUploader} from "ng2-file-upload";
|
4 |
|
5 | import {BaseComponent} from './base.component';
|
6 |
|
7 | declare var restRoot: string;;
|
8 | @Component({
|
9 | moduleId: module.id,
|
10 | selector: "iisp-file-upload",
|
11 | template: `<input type='file' ng2FileSelect [uploader]='uploader' accept='{{fileUploadInfo.allowedFileType}}' [(ngModel)]="fileUploadInfo.fileInputValue" (change)='selectedFileOnChanged($event)' />
|
12 | <span title='{{fileUploadInfo.message}}' [ngClass] = 'fileUploadInfo.iconClass'></span>`
|
13 | })
|
14 | export class FileUploadComponent extends BaseComponent{
|
15 | @Input()
|
16 | fileUploadInfo: FileUploadInfo;
|
17 | index = 0;
|
18 | public uploader = new FileUploader({
|
19 | url: restRoot + "/common/files/upload",
|
20 | method: "POST",
|
21 | itemAlias: "uploadedfile"
|
22 | });
|
23 |
|
24 | selectedFileOnChanged(event: any) {
|
25 |
|
26 | this.uploadFile(this.fileUploadInfo);
|
27 | }
|
28 |
|
29 | uploadFile(fileUploadInfo: FileUploadInfo) {
|
30 |
|
31 | console.debug("length: " + this.uploader.queue.length);
|
32 | this.uploader.queue[this.index].onSuccess = function(response, status, headers) {
|
33 | fileUploadInfo.uploading = false;
|
34 |
|
35 | if (status == 200) {
|
36 |
|
37 | console.debug(response);
|
38 | let tempRes = JSON.parse(response);
|
39 | console.debug("tempRes.uploadPaths:" + tempRes.uploadPaths);
|
40 | fileUploadInfo.uploadedFiles = fileUploadInfo.uploadedFiles.concat(tempRes.uploadPaths);
|
41 | fileUploadInfo.iconClass = "successmini";
|
42 | fileUploadInfo.message = "Uploaded";
|
43 | } else {
|
44 | fileUploadInfo.iconClass = "errormini";
|
45 | fileUploadInfo.message = "Upload failed";
|
46 | }
|
47 | };
|
48 |
|
49 | this.uploader.queue[this.index].upload();
|
50 | this.index++;
|
51 | fileUploadInfo.uploading = true;
|
52 | fileUploadInfo.iconClass = "loadingmini";
|
53 | fileUploadInfo.message = "Uploading";
|
54 | }
|
55 | isRunning(): boolean{ return false;}
|
56 | }
|
57 |
|
58 | export class FileUploadInfo {
|
59 | uploadedFiles: string[] = new Array();
|
60 | fileInputValue: string;
|
61 | uploading: boolean = false;
|
62 | iconClass = "blankmini";
|
63 | message: string;
|
64 | allowedFileType: string;
|
65 | constructor(allowedFileType?: string) {
|
66 | if (allowedFileType) {
|
67 | this.allowedFileType = allowedFileType;
|
68 | }
|
69 | }
|
70 | reset():void {
|
71 | this.fileInputValue = "";
|
72 | this.uploading = false;
|
73 | this.message = "";
|
74 | this.iconClass = "blankmini";
|
75 | }
|
76 | } |
\ | No newline at end of file |