0 Replies Latest reply on Feb 8, 2008 5:25 AM by ChiefRocka00

    File Upload Error

    ChiefRocka00 Level 1
      I am getting the following error when I try to upload a file from Flex and PHP. I created a small little php utility to validate that the upload works and it worked perfectly but when I try to run it via flex I get this IO Error. Any idea what I need to do to get this Flex upload utility working? Here is my code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#f6f6f6" backgroundGradientColors="[#f6f6f6, #bbbbbb]" creationComplete="init()">

      <mx:Label x="10" y="10" text="File Upload" fontSize="20" fontWeight="bold"/>
      <mx:HRule x="10" y="49" width="80%"/>

      <mx:Button x="10" y="59" label="Upload" click="{upload()}"/>

      <mx:Script>
      <![CDATA[
      import flash.events.DataEvent;
      import flash.events.IOErrorEvent;

      private var fileRef:FileReference = new FileReference();
      private var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");

      public function init():void {
      Security.allowDomain("*");
      }

      public function upload():void {
      // listen for the file selected event
      // listen for the upload complete event
      fileRef.addEventListener(Event.SELECT, selectHandler);
      fileRef.addEventListener(Event.COMPLETE, completeHandler);
      fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA , uploadCompleteHandler);
      fileRef.addEventListener(IOErrorEvent.IO_ERROR, onIOError)

      // browse for the file to upload
      // when user selects a file the select handler is called
      try {
      var success:Boolean = fileRef.browse([imageTypes]);
      }
      catch (error:Error) {
      textarea1.text = "Unable to browse for files.";
      //trace("Unable to browse for files.");
      }
      }

      // when a file is selected you upload the file to the upload script on the server
      public function selectHandler(event:Event):void {
      var request:URLRequest = new URLRequest(" http://www.myURL.com/fileUpload.php")
      try {
      // upload file
      fileRef.upload(request);
      textarea1.text = "uploading " + fileRef.name + "...";
      }
      catch (error:Error) {
      textarea1.text = "Unable to upload file.";
      //trace("Unable to upload file.");
      }
      }

      // dispatched when file has been given to the server script. does not receive a response from the server
      public function completeHandler(event:Event):void {
      textarea1.text = "file uploaded complete";
      //trace("file uploaded complete");
      }

      // dispatched when file has been uploaded to the server script and a response is returned from the server
      // event.data contains the response returned by your server script
      public function uploadCompleteHandler(event:DataEvent):void {
      trace("uploaded... response from server: \n" + String(event.data));
      textarea1.text += event.data as String;
      }

      public function onIOError(event:IOErrorEvent):void {
      textarea1.text = "There was an IO Error that occured: " + event;
      }

      ]]>
      </mx:Script>

      <mx:TextArea x="10" y="89" width="327" height="134" id="textarea1"/>

      </mx:Application>