-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileUploader.as
140 lines (111 loc) · 3.68 KB
/
FileUploader.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
package {
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.utils.ByteArray;
import flash.utils.Endian;
public class FileUploader extends EventDispatcher {
public function FileUploader() {
}
public static const boundaryLength:int = 0x12;
public static const defaultContentType:String = "application/octet-stream";
public static function uploadFiles(loader:URLLoader,
request:URLRequest,
files:/*ByteArray*/Array,
fileNames:/*String*/Array = null,
fieldNames:/*String*/Array = null,
contentTypes:/*String*/Array = null):void {
var req:URLRequest = packRequest(request, files, fileNames, fieldNames, contentTypes);
loader.load(req);
}
public static function packRequest(request:URLRequest,
files:/*ByteArray*/Array,
fileNames:/*String*/Array = null,
fieldNames:/*String*/Array = null,
contentTypes:/*String*/Array = null):URLRequest {
var i:int;
var tmp:String;
// generate boundary
var boundary:ByteArray = new ByteArray();
boundary.endian = Endian.BIG_ENDIAN;
boundary.writeShort(0x2d2d);
for (i = 0; i < boundaryLength-2; i++) {
boundary.writeByte(int(97 + Math.random() * 26));
}
boundary.position = 0;
// prepare new request
var req:URLRequest = new URLRequest(request.url);
req.contentType = 'multipart/form-data; boundary=' +
boundary.readUTFBytes(boundaryLength);
req.method = URLRequestMethod.POST;
// prepare post data
var postData:ByteArray = new ByteArray();
postData.endian = Endian.BIG_ENDIAN;
// write parameters
var pars:URLVariables = request.data as URLVariables;
if (pars) {
for (var par:String in pars) {
// -- + boundary
postData.writeShort(0x2d2d);
postData.writeBytes(boundary);
// line break
postData.writeShort(0x0d0a);
// content disposition
tmp = 'Content-Disposition: form-data; name="' + par + '"';
postData.writeUTFBytes(tmp);
// 2 line breaks
postData.writeInt(0x0d0a0d0a);
// parameter
postData.writeUTFBytes(pars[par]);
// line break
postData.writeShort(0x0d0a);
}
}
// write files
for (i = 0; i < files.length; ++i) {
// -- + boundary
postData.writeShort(0x2d2d);
postData.writeBytes(boundary);
// line break
postData.writeShort(0x0d0a);
// content disposition
var fieldName:String = "Filedata" + i;
if (fieldNames && fieldNames[i]) fieldName = fieldNames[i];
tmp = 'Content-Disposition: form-data; name="' + fieldName + '"; filename="';
postData.writeUTFBytes(tmp);
// file name
var fileName:String = "Filename" + i;
if (fileNames && fileNames[i]) fileName = fileNames[i];
postData.writeUTFBytes(fileName);
// missing "
postData.writeByte(0x22);
// line break
postData.writeShort(0x0d0a);
// content type
var contentType:String = defaultContentType;
if (contentTypes && contentTypes[i]) contentType = contentTypes[i];
tmp = 'Content-Type: ' + contentType;
postData.writeUTFBytes(tmp);
// 2 line breaks
postData.writeInt(0x0d0a0d0a);
// file data
var file:ByteArray = files[i];
postData.writeBytes(file/*, 0, file.length*/);
// line break
postData.writeShort(0x0d0a);
}
// end of writting files
// -- + boundary + --
postData.writeShort(0x2d2d);
postData.writeBytes(boundary);
postData.writeShort(0x2d2d);
// line break
postData.writeShort(0x0d0a);
req.data = postData;
return req;
}
}
}