0 Replies Latest reply on Oct 5, 2009 1:02 PM by Mark_B_Dev

    Help needed returning vars to FLEX from Perl when evaluating a file!

    Mark_B_Dev

      Hi All,

       

         Newbee to the Abode FLAX Forum needs help on getting info(error codes,text) back to flex while uploading a file.

       

      Here is my issue:  I need to upload a file from FLEX then start the upload in PERL and evaluate the file before I do the file upload.  I want to make sure the file is correct ( has the right data and columns in it) before I load it into my db. Thus here is my current code and if anybody has a way for PERL to talk to FLEX I would love to see it.

       

       

       

      FLEX code:

       

               <mx:Button y="254" label="Upload" click="uploadFile()" styleName="dataEntryButtonColor" right="103"/>

       

            ActionScript:

       

                  public var fileRef:FileReference = new FileReference();
                  private var request:URLRequest = new URLRequest( CGIURL + "event_contacts_upload.cgi" );
                         
                 
                  private function uploadFile( ):void {            
                      var docFilter:FileFilter = new FileFilter( "Documents", "*.txt;*.csv" )

                      fileRef.addEventListener(Event.SELECT, selectHandler);           
                      fileRef.browse( [docFilter] );
                  }
                 
                  private function selectHandler( event:Event ):void {
                      upload();

                  }
                 
                  private function upload():void {
                      var params:URLVariables = new URLVariables();
                     
                      params.sessionid = mx.core.Application.application.sessionid;
                      params.event_id = mx.core.Application.application.global_event_id;
                      params.user_type = user_type.selectedValue;
                      params.class_type = class_type.selectedValue;
                      request.method = URLRequestMethod.POST;
                      request.data = params;
                             
                      fileRef.addEventListener(Event.COMPLETE, completeHandler);
                      fileRef.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
               
                      try {
                          fileRef.upload(request);
                      } catch(error:Error) {
                          Alert.show("Upload of CSV failed","Error:");
                          return;   
                      }
                  }

                private function ioErrorHandler( event:Event ):void {
                      Alert.show( "Upload of CSV failed due to IO error","Error:" );
                  }
                 
                  private function completeHandler( event:Event ):void {
                      dispatchEvent( new Event( "uploadComplete", true ) );
                      Alert.show( "DONE:File Upload Complete", "Notice:" );
                   }

       

       

       

      PerL Code:

       

      #!/usr/bin/perl

       

      use strict;
      use CGI;
      use File::Basename;
      use common;
      use db;
      use defines;
      use user;
      use events;

       

      $|=1;

       

      my $program = basename($0) . ' ';

       

      { # Main
           
          my $dbh = db_open() || die_with_fault_xml($program . $common::errstr, 'Server');
          my $query = new CGI;

       

          my $sql;
          my @parameters = ();
          my %dbrh;
         
          my $event_id = $query->param('event_id') || die_with_fault_xml( $program . "No event specified" );
          my $user_type = $query->param('user_type') || die_with_fault_xml( $program . "No user_type specified" );
          my $class_type = $query->param('class_type') || die_with_fault_xml( $program . "No class_type specified" );

         // inserted here is where I want to evaluate the file and make sure its correct!

                      if (correct)

                              let the completeHandler tell the user using a alert.show that the file was correct and loaded!

                     else

                              this is where I want to tell the user the file upload failed because the file didnt have the correct amount of data in it.

                              My goal is to do this in a alert.show or the existing panel that is in the FLEX page. All other errors are handle by

                              the fileRef.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler ); call.

       

                GOAL: Is to let the user (person uploading the file) know why the file failed to load ( wrong column count, missing data,...)

       

      If anyone has any ideas on how to make PERL talk to FLEX I would love to here it.

       

      Thanks in Advance,

         Mark