Multipart form data in as3 [Class version 1.2]

During my last project I faced a problem to upload multipart/form-data to server using Flash. I need to do it using ActionScript only. The idea was to send to server an image generated in flash and several variables but all this data had to be POST.
I found several projects that were using Flash Socket to send multipart requests to server but the problem was that sockets required custom server configuration to work. As you understand we cant ask remote service to reconfigure their system only because we need it.
So the only way was to build your own custom URLLoader class (not socket) to send multipart form data. Ok, no more words (I hate all this typings) here is simple but usefull (as I think) MultipartURLLoader class. You will find source and small samples with php script inside.
Unfortunately there is a problem getting progress event. We cant show any progress during sending data to server because flash doesnt support it. (the same problem with sockets)

LATEST VERSION UPLOADED TO GOOGLE CODE

UPDATES:
version 1.1
– Added options for MIME-types (default is application/octet-stream)
version 1.2
– Added clearVariables and clearFiles methods
– Small code refactoring
– Public methods documentaion

in short we can add variables and files to loader class:

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
import flash.utils.ByteArray;
import ru.inspirit.net.MultipartURLLoader;
 
var data1:ByteArray = new ByteArray();
data1.writeUTFBytes("This is a test 1");
data1.position = 0;
 
var data2:ByteArray = new ByteArray();
data2.writeUTFBytes("This is a test 2");
data2.position = 0;
 
var ml:MultipartURLLoader = new MultipartURLLoader();
ml.addEventListener(Event.COMPLETE, onReady);
 
function onReady(e:Event):void
{
	// Upload Complete
}
 
// simple string data
ml.addVariable('test', 'test variable');
 
// file data: ByteArray, File name, Name of the file field, content MIME type (default application/octet-stream)
// use [] if you need identical file field name
// specify MIME type for your file part
ml.addFile(data1, 'test1.txt', 'Filedata[]');
ml.addFile(data2, 'test2.txt', 'Filedata[]', 'text/plain');
 
ml.load('test.php');