UNPKG

2.58 kBPlain TextView Raw
1import {Component, Input} from "@angular/core";
2
3import {FileUploader} from "ng2-file-upload";
4
5import {BaseComponent} from './base.component';
6
7declare 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})
14export 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 //fileUploadInfo.uploadedFiles = null;
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
58export 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