Flex 3: Upload multiple files via POST with AS3
In my current Abobe Flex project, I needed to upload multple generated files and form fields to a server. UploadPostHelper by Jonathan Marston is a great for uploading one file at time, but sadly I couldn't use (or even modify) it - it's licensed under NonCommercial CC, and my project is a commercial one.
So I had to write my own code which I would like to share with other's (dual-licensed under MPL 1.1 and LGPL 2.1).
Usage of HttpPostData-class: <Update April 1st 2010>I had an error in this text for a half year and nobody noticed? Strange...</Update>
- Create an Instance of HttpPostData:
var postData:HttpPostData = new HttpPostData()
- Add your parameters and file attachments
- postData.addParameter('foo1', 'bar1'); // POST-Field 'foo1' has value 'bar1'
- postData.addParameter('foo2', 'bar2'); // POST-Field 'foo2' has value 'bar2'
- // POST-Field 'uploadedFile1' contains someBinaryData1 (ByteArray)
- // as 'application/octet-stream' with filename 'uploadedFile1'
- postData.addFile('uploadedFile1', someBinaryData1);
- // POST-Field 'uploadedFile2' contains someBinaryData2 (ByteArray)
- // as 'image/png' with filename 'image.png'
- postData.addFile('uploadedFile2', someBinaryData2, 'image.png', 'image/png')
- Close this object. This will append the ending boundary to the data which makes it impossible to add additional form data.
- Bind this to an URLRequest
- Send the request