Friday, December 21, 2007

Flex multipart/form-data post request

Flex 2 (and 3) are great tools Adobe sent us to try and fix the harm they did with AS2 ;-)
Almost everything we needed was there, except one thing (acctually more...)
There is no way to create a multipart form in order to send any data we want to the server (we're talking ByteArray).
So I wrote a class, It's a lot like UrlRequest, accepting MultipartVariables object as data, here's a short example:

public function startUpload():void {
loader = new MultipartLoader('');
var variables:MultipartVariables = new MultipartVariables();
var myByteArray:ByteArray = new ByteArray();
// we will create a fake file content here, you should replace this
// with th ebinary data you want to upload
for(var i:int=0; i<100000;i++) {
variables.add('another_var','extra data');
loader.variables = variables;

private function fileUploaded(event:Event):void {

The code can be fetched using svn at:

or in the google code page:

Would love to hear your comments.


Raz said...

good work!


Andrew said...

Can we post multiple JPEGEncoded data as found on this tutorial ( with this?

I'd like to post multiple user-generated images all at the same time.

ebaggg said...

How do you get access to the byte data to upload the file?! If I use FileReference to browse() and select a file, I don't have access to the bytes.

Mike Stead said...

Hey Neer, I had a pretty detailed look over this problem recently too. You can check the results out here.

Django said...

relative paths not allowed. fooey... i just can't seem to get it to work with rails. I am not sure what I am doing wrong

xxxx said...

Hi NeerFri.

Where is the last version of
MultipartLoader.swc file?

I try to use it but without the parameter filename="ruby.pdf" I only pass a parameter called data name="data";

Kind regards and thank you four your help.

bh5k said...

Any idea as to why no matter what I do I get a sandbox violation error. I have added a crossdomain file and granted everything access.

Mike Stead said...

Thought I'd come back and mention the link I posted in a previous comment is now dead, but you can now find it here:

Antonius said...

i'm very new in adobe flex/air,
your post is very interesting, but i don't know where loader comes from

Ganesh Bhat said...

Nice work.
But how can I set cookie on multipart request?

My application does authentication based on that.

Please help


Sairam said...

Hi Mike,

Today, I just tried the link you have given below but I am getting the following error:

Error establishing a database connection


Mike Stead said...

Hey sorry about that, I'm having issues with my hosting.

You can find the full source to my solution along with an example and unit tests here:

Mike Stead said...

Also the cached version of the blog page can be found here:

Nirmal Kumar Bhogadi said...

Hello Neer,

I am working on an application where in I need to send the multipart data from flex to .NET. The problem is I will not have any URL here. I just need to generate the multipart content and send it to .NET. Can you please suggest me what can I do in this respect.

Nirmal Kumar Bhogadi

Open Development Forum said...
This comment has been removed by the author.
[Charith De Silva] said...

Big thanks to your project. Its just great and resolved my impediment.