使用URLRequest上传文件 upload files using URLRequest
原文地址:http://www.pavlasek.sk/devel/?p=10
?
In Flash player it was possible until version 10 to upload files only by FileReference.upload method (there were some ugly hacks – such as uploading in external iframe).
Uploading files using directly FileReference.upload has some serious problems problems. Especially on Firefox, Flash player plugin creates it’s own request (different UserAgent, session and so) so on server it is handled in different session. All the parameters has to be sent, or some hacks has to be done (I manually set the jsessionid to each request).
And after all known hacks, it has still some serious problems using SSL – if the certificate is not validly imported to client machine, it just crashes without asking, if it has to trust it and so. See bugs:
http://bugs.adobe.com/jira/browse/FP-201http://bugs.adobe.com/jira/browse/FP-226http://bugs.adobe.com/jira/browse/SDK-13701http://bugs.adobe.com/jira/browse/SDK-13196Since v. 10 (and Flex 4), there is possibility to read local files directly, so I decided to try to upload files not using FileReference.upload method. But to read data as ByteArray and send it as POST request to server.
I found interesting class -UploadPostHelper in this article. Next I merged that example with my old code – browse for file and changed the method from FileReference.upload to FileReference.load. Server is java servlet using apache commons FileUpload – saves file to temp folder and sends XML response – all form items from post request and file uploaded – name and absolute path.
Adobe has changed security in flash player 10, it is impossible to send files in background (probably all requests now needs user interaction). It has to be in some user-action (click…) event handler, so I have two buttons, one for browse for a file, one for upload. Using just method that handles Event.COMPLETE (FileReference) causes Security sandbox violation.
Here is my Client code (UploadTest.mxml):
?
?