File Upload with Multipart Request - revisited

Monday, March 9, 2009

In the file upload chapter of the book I show how to create a multipart request to upload an image to a Rails server. On a client project I needed to send some XML and two images in one post to a Rails application. Mike, over at, wrote a nice framework to create multipart requests in an ActionScript like fashion which would make this easier. You can read the whole article over at However, I tried his out and it didn't work with Rails. After some debugging and a little tweaking I made it work, you will find the fix at the end of this article. So let's look at an example (from my client project, used without authorization ;-):

private function sendToServer(planId:String, plan:XML, patternBitmap:Bitmap, layoutBitmap:Bitmap):void {
var variables:URLVariables = new URLVariables();
variables.quilt_plan = plan.toXMLString();
variables.pattern_image = new URLFileVariable(new PNGEncoder().encode(patternBitmap.bitmapData), "pattern_image.png");
variables.layout_image = new URLFileVariable(new PNGEncoder().encode(layoutBitmap.bitmapData), "layout_image.png");
var request:URLRequest = new URLRequestBuilder(variables).build();
request.url = "/quilt_plans/";
request.method = URLRequestMethod.POST;

loader = new URLLoader(request);
loader.addEventListener(Event.COMPLETE, completeHandler);

As you see using the URLFileVariable and URLRequestBuilder is pretty straight forward. In the above example we first add some XML to the URLVariable:

variables.quilt_plan = plan.toXMLString();

Then we add the bitmap data of an image:

variables.pattern_image = new URLFileVariable(new PNGEncoder().encode(patternBitmap.bitmapData), "pattern_image.png");

We add a second image named layout_image in the similar manner. Finally we use the build method of the URLRequestBuilder to create a multipart request.

var request:URLRequest = new URLRequestBuilder(variables).build();

This request can then be passed to the load method of a standard URLLoader to send two images and an XML document in one swoop.

On the Rails side in our controller

    @quilt_plan = => params[:quilt_plan])
@quilt_plan.build_layout_image(:uploaded_data => params[:layout_image])
@quilt_plan.build_pattern_image(:uploaded_data => params[:pattern_image])

Now the params argument passed to your Rails action method contains the XML in the params[:quilt_plan] variable, and the two images ready for upload in params[:layout_image] and params[:pattern_image]. You could do params[:layout_image].read to get the image, or as with our example the attachment_fu plugin does it for us.

Et voila...happy multipart requesting!

Rails did have issues with the way the request was created and I applied the following changes which mainly just changes the Line Feed send in the requests in

< private static const LF:String = "\r\n";
> private static const LF:String = "\n";



Blogger Unknown said...

Thanks for the informative post.

At the time of writing, the domain you reference is not resolving (so the original code is not available except through eg google cache).

It might be helpful if you provided a copy of your modified code in its entirety.

David Lee

June 30, 2009 at 6:10 PM  
Blogger Unknown said...

Sorry I've had to moved domain as had issues with my previous one. You can now find the referenced blog post, along with updated code, here:


October 7, 2009 at 5:54 AM  
Blogger Unknown said...


thx for the post. I'm still struggeling with the request headers, maybe you can give me a hint:

i have set them with:

new URLRequestHeader("Content-Type", "application/xml;")

but it sends the xml i wanna transport encoded:


the post data shows the correct request header:

Content-type: application/xml;

in FF the actualy header says:

"Content-Type text/html"

any ideas ?


December 30, 2009 at 7:12 AM  
Blogger Unknown said...

ok, adobe says:

If the value of the data property is a URLVariables object, the value of contentType must be application/x-www-form-urlencoded.

but how do you parse the xml on the rails side ?

December 30, 2009 at 9:18 AM  
Blogger Unknown said...

problem solved :)

i can do:


to access the params...

December 30, 2009 at 3:47 PM  
Blogger daijongaddis said...

WynnBET - Las Vegas Casino - JSHub
WynnBET - 여주 출장마사지 Las Vegas Casino · 1-800-522-4700. Encore at Wynn Las Vegas 광명 출장마사지 has 전주 출장안마 a full 목포 출장샵 menu of table games, 경상남도 출장샵 including blackjack, roulette, craps,

March 4, 2022 at 4:25 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

© 2008 Flex On Rails. All Rights Reserved