-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUploader.as
186 lines (138 loc) · 4.51 KB
/
Uploader.as
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
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
package {
import flash.net.*;
import flash.events.*
public class Uploader {
private var __types:FileFilter
private var __allTypes:Array
private var __fileRefLista:FileReferenceList;
private var __fileRefItem:FileReference;
private var __uploadURL:URLRequest;
private var __lista:Array; // lista dos arquivos
private var __listaIndice:Number;//Numero do arquivo atual
public var totalBytes:Number;
public var uploadedBytes:Number
public var percentUploaded:Number
public var verbose:Boolean = false;
/* eventos */
public var onSelect:Function
public var onProgress:Function
public var onComplete:Function
public var onError:Function
public function Uploader($descricao:String, $tipos:Array, $urlDestino:String) {
__types = new FileFilter($descricao, $tipos.join('; '))
__allTypes = []
__allTypes.push(__types);
__uploadURL = new URLRequest();
__uploadURL.url = $urlDestino;
__fileRefLista = new FileReferenceList();
__fileRefLista.addEventListener(Event.SELECT, __onSelect);
}
private function __onSelect(e:Event):void {
__lista = __fileRefLista.fileList;
__startUpload();
try {
onSelect(); //calback
} catch (e:Error){
}
}
private function __startUpload():void {
__trace("iniciando upload");
/* calcula total de bytes a serem enviados */
totalBytes = 0;
uploadedBytes = 0;
percentUploaded = 0;
var item:FileReference
for (var i:String in __lista) {
item = __lista[i]
totalBytes += item.size;
}
item = null;
__trace('bytes to upload = ' + totalBytes);
__listaIndice = -1;
__uploadNext()
}
private function __uploadNext() {
if (__listaIndice + 1 < __lista.length) { //ainda há arquivos...
__listaIndice++
if (__fileRefItem) {
// remove listeners anteriores
__fileRefItem.removeEventListener(ProgressEvent.PROGRESS, __onProgress);
__fileRefItem.removeEventListener(IOErrorEvent.IO_ERROR, __onError);
__fileRefItem.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, __onError);
__fileRefItem.removeEventListener(HTTPStatusEvent.HTTP_STATUS, __onErrorHttp);
} else {
__fileRefItem = new FileReference();
}
__fileRefItem = FileReference(__lista[__listaIndice]); //pega a referencia do item
//adiciona listeners
__fileRefItem.addEventListener(ProgressEvent.PROGRESS, __onProgress);
__fileRefItem.addEventListener(IOErrorEvent.IO_ERROR, __onError);
__fileRefItem.addEventListener(SecurityErrorEvent.SECURITY_ERROR, __onError);
__fileRefItem.addEventListener(HTTPStatusEvent.HTTP_STATUS, __onErrorHttp);
__fileRefItem.upload(__uploadURL);
__trace('subindo... ' + __fileRefItem.name + ' / ' + __fileRefItem.size);
} else { //...terminou
__trace('fim do upload');
try {
onComplete();
} catch (e:Error){
}
}
}
private function __onError(e:*):void {
__trace(e.text);
try {
onError(e.text)
} catch (e:Error){
}
}
private function __onErrorHttp(e:HTTPStatusEvent):void {
__trace('Erro HTTP :' + e.status);
try {
onError(e.status)
} catch (e:Error){
}
}
private function __onProgress(event:ProgressEvent) {
var file:FileReference = FileReference(event.target);
var bytesLoaded:Number = event.bytesLoaded
var bytesTotal:Number = event.bytesTotal
var b:Number
if (bytesLoaded == bytesTotal) {
//completou o upload do arquivo
uploadedBytes += bytesLoaded
b = uploadedBytes;
} else {
//upload parcial do arquivo
b = uploadedBytes + bytesLoaded;
}
percentUploaded = (b*100)/totalBytes
try {
onProgress(percentUploaded, file.name);
} catch (e:Error){
}
if (bytesLoaded == bytesTotal) {
//completou o upload do arquivo // dispara o onProgress antes de carregar o proximo
__uploadNext();
}
}
public function browse() {
__fileRefLista.browse(__allTypes)
}
public function cancel() {
__fileRefItem.cancel();
}
public function get files():Array {
var tmp = []
for (var i:Number = 0; i < __lista.length; i++) {
tmp.push(__lista[i].name)
}
return tmp;
}
private function __trace($texto):void{
if (verbose==true) {
Tracebox.msg('Uploader:' + $texto)
}
}
}
}