2 Replies Latest reply on Feb 24, 2010 11:18 AM by Yozef0

    HTML Upload vs. Flex FileReference

    Yozef0 Level 1
      I am trying to upload a file from Flex with PHP, I am able to do it in HTML as so:
      <html>
      <body>
       
      <form action="service.php?type=uploadattachment" method="post"
      enctype="multipart/form-data">
      <label for="file">Filename:</label>
      <input type="file" name="file" id="file" />
      <br />
      <input type="submit" name="submit" value="Submit" />
      <input  type="hidden" value="file" name="uploadfile" />
      <input  type="hidden" value="file.php" name="savefile" /> 
      </form>
       
      </body>
      </html>
      

       

       

      In Flex:

       

      private var uploadURL:URLRequest;
      private var file:FileReference;
                        
            private function attachFile():void {
                   uploadURL = new URLRequest();
                   uploadURL.method = URLRequestMethod.POST;
                  uploadURL.url = "http://xxxxxxxx/ADTOOL/controllers/service.php?type=uploadattachment";
                  file = new FileReference();
                  configureListeners(file);
                  file.browse(getTypes());
             }
            
            private function configureListeners(dispatcher:IEventDispatcher):void {
                  dispatcher.addEventListener(Event.CANCEL, cancelHandler);
                  dispatcher.addEventListener(Event.COMPLETE, completeHandler);
                  dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
                  dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                  dispatcher.addEventListener(Event.OPEN, openHandler);
                  dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                  dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                  dispatcher.addEventListener(Event.SELECT, selectHandler);
                  dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);
             }
      
             private function getTypes():Array {
                  var allTypes:Array = new Array(getImageTypeFilter());
                  return allTypes;
             }
      
             private function getImageTypeFilter():FileFilter {
                  return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png, *.zip, *.doc, *.pdf)", "*.jpg;*.jpeg;*.gif;*.png;*.zip*.doc;*.pdf");
             }
      
             private function cancelHandler(event:Event):void {
                  trace("cancelHandler: " + event);
             }
      
             private function completeHandler(event:Event):void {
                  trace("completeHandler: " + event);
             }
      
             private function uploadCompleteDataHandler(event:DataEvent):void {
                  trace("uploadCompleteData: " + event);
             }
      
             private function httpStatusHandler(event:HTTPStatusEvent):void {
                  trace("httpStatusHandler: " + event);
             }
             
             private function ioErrorHandler(event:IOErrorEvent):void {
                  trace("ioErrorHandler: " + event);
             }
      
             private function openHandler(event:Event):void {
                  trace("openHandler: " + event);
             }
      
             private function progressHandler(event:ProgressEvent):void {
                  var file:FileReference = FileReference(event.target);
                  trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
             }
      
             private function securityErrorHandler(event:SecurityErrorEvent):void {
                  trace("securityErrorHandler: " + event);
             }
      
             private function selectHandler(event:Event):void {
                  var file:FileReference = FileReference(event.target);
                  trace("selectHandler: name=" + file.name + " URL=" + uploadURL.url);
                  file.upload(uploadURL, "file");
             }
      

       

      <mx:Button label="File" click="attachFile()" width="100%"/>
      

       

      It fires the UploadCompleteData but it's not being uploaded on the server. There are values in the HTML not being passed in Flex... is there a way for me to add those?

        • 1. Re: Backend: service.php
          Yozef0 Level 1
          <?php
          
          
               
               if (isset($_GET['type'])) {
                    switch ($_GET['type']) {
                         case ("uploadattachment"): {
                              /**
                              * type :    
                              *           uploadattachment
                              * post :
                              *   uploadfile      // http file name
                              *   savefile        // full path with file name   
                              */
                              $upload_max_size = 1024 * 100000; // 100MB   
                              $success_message = "saved";
                              $failed_message = "failed";
                              if (isset($_POST['uploadfile']) && isset($_POST['savefile'])  && !($_FILES[$_POST['uploadfile']]["error"] > 0) && !empty($_FILES[$_POST['uploadfile']]) &&  $_FILES[$_POST['uploadfile']]['size'] <= $upload_max_size) {                              
                                   if(move_uploaded_file($_FILES[$_POST['uploadfile']]['tmp_name'], $_POST['savefile'])) {
                                        echo $success_message;
                                   }
                                   else {
                                        echo $failed_message;                          
                                   } 
                              }
                              else {
                                   echo $failed_message;                     
                              } 
                              break;
                         }
                    }            
               }
          ?>
          

           

          and I keep getting 'failed' after uploadEventComplete

          • 2. Re: Backend: service.php
            Yozef0 Level 1

            Solved:

             

            The problem was I wasn't passing the following params to the service.php :

             

             

             

            Solution is:

            private function attachFile():void {
                 var sendVars:URLVariables = new URLVariables();
                        sendVars.savefile = "file.php";
                        sendVars.uploadfile = "file";
                                   
                 uploadURL = new URLRequest();
                 uploadURL.method = URLRequestMethod.POST;
                 uploadURL.data = sendVars;
                 uploadURL.url = "http://xxxxxxxx/ADTOOL/controllers/service.php?type=uploadattachment";
                 file = new FileReference();
                 configureListeners(file);
                 file.browse(getTypes());
            }