FileReference Bug in Flash Player 9 for Mac OS
We found this one at work last week while doing some test for an applications that requires tracking of user uploads. On Flash Player 9 for the Mac, even though tracking progress of the upload went smoothly, the “complete” event failed to trigger every time. Apparently this one was around on Flash Player 8 as well, but the only suggested solution we found, to return an empty string, did not work for Flash Player 9.
We realized the script handling the upload needs to actually output something for the “complete” event to trigger on the Flash Player 9 on the Mac, if even a blank space. However, I decided that if we are going to return something, it’d better be something we can actually use.
In addition to the “uploadComplete” event, the FileReference class has an “uploadCompleteData” event, which can be used to capture the response sent by the script, once the file has been uploaded (or not, depends on your goals). So, here is a simple example of how that may be accomplished with PHP: the following script handles a file upload and returns a string containing the current session id.
upload.php
1 2 3 4 5 6 7 8 9 | <?php session_start(); // Not needed if your server is set to start sessions automatically move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$_FILES['Filedata']['name']); echo "sessionID=" . session_id(); ?> |
The next AS3 code uses the previous PHP script to save a file to the server. Once the transaction is completed, it will print the value of the sessionID variable to the Output panel.
UploadTest.as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package { import flash.events.*; import flash.net.FileReference; import flash.net.URLRequest; import flash.net.URLVariables; public class UploadTest { var fileRef:FileReference; function UploadTest() { init(); } private function init():void { fileRef = new FileReference(); fileRef.addEventListener("select", onSelect); fileRef.addEventListener("complete", onComplete); fileRef.addEventListener("uploadCompleteData", onUploadCompleteData); try { fileRef.browse(); } catch (error:Error) { trace("Unable to browse for files."); } } private function onSelect(event:Event):void { var request:URLRequest = new URLRequest("path/to/upload.php"); try { fileRef.upload(request); } catch (error:Error) { trace("Unable to upload file."); } } private function onComplete(event:Event):void { var file:FileReference = FileReference(event.target); trace("completeHandler: " + file.name + "n"); } private function onUploadCompleteData(event:DataEvent):void { var vars:URLVariables = new URLVariables(event.data); trace(vars.sessionID); // prints the variable value } } } |
Comments
No Comments
Leave a reply