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('http://my.uploads.server.com:3007/uploads/test');
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++) {
myByteArray.writeUTFBytes("123by\nAR\r\nRA\x65\x0156");
}
variables.add('file_data',myByteArray);
variables.add('another_var','extra data');
loader.variables = variables;
loader.addEventListener(Event.COMPLETE,fileUploaded);
loader.load();
}

private function fileUploaded(event:Event):void {
trace(MultipartLoader(event.target).responseBody);
}

The code can be fetched using svn at: https://multipart-loader.googlecode.com/svn/trunk/multipart-loader

or in the google code page:
http://code.google.com/p/multipart-loader/downloads/list

Would love to hear your comments.

23 comments:

Raz said...

good work!

10x

Unknown said...

Can we post multiple JPEGEncoded data as found on this tutorial (http://henryjones.us/articles/using-the-as3-jpeg-encoder) 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.

Unknown said...

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

ProfessorX 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.
Reiner

Unknown 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.

Unknown 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:

http://blog.mikestead.me/upload-multiple-files-with-a-single-request-in-flash/

Unknown said...

hi,
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

Thanks
Ganesh

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

Link: http://blog.mikestead.me/upload-multiple-files-with-a-single-request-in-flash/

Unknown 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:

http://github.com/mikestead/as3urlrequestbuilder

Unknown said...

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

http://webcache.googleusercontent.com/search?q=cache:6HnouWSH83oJ:blog.mikestead.me/upload-multiple-files-with-a-single-request-in-flash/+as3+url+request+mike+stead&cd=1&hl=en&ct=clnk&gl=au

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.

Thanks,
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.

balbiroakey said...

Casino review, bonus, and casino games - Dr.MCD
You 순천 출장안마 can also play with the bonus, the live casino, 양산 출장샵 and even 군산 출장샵 online poker. There's a variety of games, 안양 출장마사지 including American Roulette, 광주광역 출장안마

Unknown said...

The History of the Casino - One of the Most Popular Casinos
A relative newcomer to the world of online herzamanindir.com/ gambling, Wynn Las Vegas goyangfc.com opened its doors to a new audience of over 출장마사지 600,000 in 2017. This casinosites.one was the first casino mens titanium wedding bands

Anonymous said...

Handpicked, high-RTP jackpot games and the best software providers are variety of the} causes to sign-up now. Attractive as they 솔 카지노 could appear, they'll cost you money in the long run|the long term}. Like betting on Tie or Player, you're making your chances of bagging a profitable session much less doubtless (it’s the identical in blackjack).

i0gm7rt9f4 said...

DCS could present the functionality of greater than one of many aforementioned DCS sorts (e.g., DCS 3/3/1 which mixes functionality of DCS 원 엑스 벳 3/3 and DCS 3/1). For such DCS, the necessities will be, a minimum of|no much less than}, the aggregation of requirements on the "part" DCS. Interconnection is between a DSX or LGX to a Switch, one other Cross Connection, or different service platform device. Online means transactions via electronic data-interchange whether actual time transactions or in any other case, which can be via the internet, intranet networks and the like. For every spin, the dual reels can broaden to turn into three, four or even five-doubled reels.

bwt1hrogne said...

Decreasing layer peak for all 3D printers investigated in this research elevated estimated print time and decreased estimated price excluding Stratasys F370, in which estimated price elevated. The Stratasys Oversized Sweater for Fall and Winter J750 Digital Anatomy print settings of High Speed and High Mix permits for the discount of estimated print time and price. The LulzBot Mini 2 is an efficient alternative for people, faculties, and neighborhood facilities because of its simple setup and operation. Hobbyists and tinkerers will like its capability to print with broad variety|all kinds} of filament varieties. The development of additive manufacturing may have a large impression on the setting.

n9ejooyjmr said...

Excellent warmth dissipation performance, excessive luminous upkeep fee, uniform gentle emission, and no spots. COB gentle source has Standard Toilet Parts proved itself to be a incredible improve that ensures each printing comes out completely. The power of the machine has been repeatedly verified to make sure printing safety and cut back printing errors.

9et2fx4okv said...

SPRUE– Feed opening provided in the injection or transfer mould; also the slug formed at this hole. SPLAY MARKS– Lines discovered partially after molding, often because of of} move of material in Patio Heater mould. SPIN WELDING– A process of fusing two objects collectively by forcing them collectively while one of the pair is spinning, until frictional heat melts the interface. Spinning is then stopped and pressure held until they're frozen collectively.