Browse for file and upload


/ Published in: MXML
Save to your folder(s)

found this on the adobe cookbook site. There is also some PHP to go along with this in my snippets.


Copy this code and paste it in your HTML
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();">
  3. <mx:Script>
  4. <![CDATA[
  5.  
  6. private var urlRequest:URLRequest;
  7. private var fileReferenceList:FileReferenceList;
  8. private var serverSideScript:String = "http://localhost/uploadFile.php";
  9.  
  10. private function init():void {
  11. urlRequest = new URLRequest(serverSideScript);
  12. fileReferenceList = new FileReferenceList();
  13. fileReferenceList.addEventListener(Event.SELECT, fileSelectedHandler);
  14. }
  15.  
  16. private function uploadFile():void {
  17. fileReferenceList.browse();
  18. }
  19.  
  20. private function fileSelectedHandler(event:Event):void {
  21. var fileReference:FileReference;
  22. var fileReferenceList:FileReferenceList = FileReferenceList(event.target);
  23. var fileList:Array = fileReferenceList.fileList;
  24.  
  25. // get the first file that the user chose
  26. fileReference = FileReference(fileList[0]);
  27.  
  28. // upload the file to the server side script
  29. fileReference.addEventListener(Event.COMPLETE, uploadCompleteHandler);
  30. fileReference.upload(urlRequest);
  31.  
  32. // update the status text
  33. statusText.text = "Uploading...";
  34. }
  35.  
  36. private function uploadCompleteHandler(event:Event):void {
  37. statusText.text = "File Uploaded: " + event.target.name;
  38. }
  39.  
  40. ]]>
  41. </mx:Script>
  42.  
  43. <mx:Label text="Upload File From Flex to PHP" fontWeight="bold"/>
  44. <mx:Label text="Choose a file..." id="statusText"/>
  45. <mx:Button click="uploadFile();" label="Upload File"/>
  46.  
  47. </mx:Application>

URL: http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&postId=5241&productId=2

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.