forked from valor-software/ng2-file-upload
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-item.class.ts
154 lines (132 loc) · 4.06 KB
/
file-item.class.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
153
154
import { FileLikeObject } from './file-like-object.class';
import { FileUploader, ParsedResponseHeaders, FileUploaderOptions } from './file-uploader.class';
export class FileItem {
public file:FileLikeObject;
public _file:File;
public alias:string;
public url:string = '/';
public method:string;
public headers:any = [];
public withCredentials:boolean = true;
public formData:any = [];
public isReady:boolean = false;
public isUploading:boolean = false;
public isUploaded:boolean = false;
public isSuccess:boolean = false;
public isCancel:boolean = false;
public isError:boolean = false;
public progress:number = 0;
public index:number = void 0;
public _xhr:XMLHttpRequest;
public _form:any;
protected uploader:FileUploader;
protected some:File;
protected options:FileUploaderOptions;
public constructor(uploader:FileUploader, some:File, options:FileUploaderOptions) {
this.uploader = uploader;
this.some = some;
this.options = options;
this.file = new FileLikeObject(some);
this._file = some;
if (uploader.options) {
this.method = uploader.options.method || 'POST';
this.alias = uploader.options.itemAlias || 'file';
}
this.url = uploader.options.url;
}
public upload():void {
try {
this.uploader.uploadItem(this);
} catch (e) {
this.uploader._onCompleteItem(this, '', 0, {});
this.uploader._onErrorItem(this, '', 0, {});
}
}
public cancel():void {
this.uploader.cancelItem(this);
}
public remove():void {
this.uploader.removeFromQueue(this);
}
public onBeforeUpload():void {
return void 0;
}
public onBuildForm(form:any):any {
return {form};
}
public onProgress(progress:number):any {
return {progress};
}
public onSuccess(response:string, status:number, headers:ParsedResponseHeaders):any {
return {response, status, headers};
}
public onError(response:string, status:number, headers:ParsedResponseHeaders):any {
return {response, status, headers};
}
public onCancel(response:string, status:number, headers:ParsedResponseHeaders):any {
return {response, status, headers};
}
public onComplete(response:string, status:number, headers:ParsedResponseHeaders):any {
return {response, status, headers};
}
public _onBeforeUpload():void {
this.isReady = true;
this.isUploading = true;
this.isUploaded = false;
this.isSuccess = false;
this.isCancel = false;
this.isError = false;
this.progress = 0;
this.onBeforeUpload();
}
public _onBuildForm(form:any):void {
this.onBuildForm(form);
}
public _onProgress(progress:number):void {
this.progress = progress;
this.onProgress(progress);
}
public _onSuccess(response:string, status:number, headers:ParsedResponseHeaders):void {
this.isReady = false;
this.isUploading = false;
this.isUploaded = true;
this.isSuccess = true;
this.isCancel = false;
this.isError = false;
this.progress = 100;
this.index = void 0;
this.onSuccess(response, status, headers);
}
public _onError(response:string, status:number, headers:ParsedResponseHeaders):void {
this.isReady = false;
this.isUploading = false;
this.isUploaded = true;
this.isSuccess = false;
this.isCancel = false;
this.isError = true;
this.progress = 0;
this.index = void 0;
this.onError(response, status, headers);
}
public _onCancel(response:string, status:number, headers:ParsedResponseHeaders):void {
this.isReady = false;
this.isUploading = false;
this.isUploaded = false;
this.isSuccess = false;
this.isCancel = true;
this.isError = false;
this.progress = 0;
this.index = void 0;
this.onCancel(response, status, headers);
}
public _onComplete(response:string, status:number, headers:ParsedResponseHeaders):void {
this.onComplete(response, status, headers);
if (this.uploader.options.removeAfterUpload) {
this.remove();
}
}
public _prepareToUploading():void {
this.index = this.index || ++this.uploader._nextIndex;
this.isReady = true;
}
}