1 Reply Latest reply on Aug 29, 2010 11:46 AM by Shongrunden

    Upload Component, Pass extra Variable

    djh88ukwb Level 1

      Hello all.

       

      I have been trying to get an upload component to work for some time now, but have been having a few troubles getting one to do what i need.  Recently i found a component that does pretty much everything i want bar one thing, pass an extra variable to the PHP script so i can name the files something unique.

       

      My Flex code is -

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx"
                     creationComplete="init()" width="600" height="300">
          <fx:Script>
              <![CDATA[
                 
                  private var urlRequest:URLRequest;
                  private var fileReferenceList:FileReferenceList;
                  private var serverSideScript:String = "http://www.davidtest.webcastglobal.com/upload/upload.php";
                  [Bindable] public var ID:String;
                 
                  private function init():void {
                      urlRequest = new URLRequest(serverSideScript);
                      fileReferenceList = new FileReferenceList();
                      fileReferenceList.addEventListener(Event.SELECT, fileSelectedHandler);
                  }
                 
                  private function uploadFile():void {
                      fileReferenceList.browse();
                  }
                 
                  private function fileSelectedHandler(event:Event):void {
                      var fileReference:FileReference;
                      var fileReferenceList:FileReferenceList = FileReferenceList(event.target);
                      var fileList:Array = fileReferenceList.fileList;
                     
                      // get the first file that the user chose
                      fileReference = FileReference(fileList[0]);
                     
                      // upload the file to the server side script
                      fileReference.addEventListener(Event.COMPLETE, uploadCompleteHandler);
                      fileReference.upload(urlRequest);
                     
                      // update the status text
                      statusText.text = "Uploading...";
                  }
                 
                  private function uploadCompleteHandler(event:Event):void {
                      statusText.text = "File Uploaded: " + event.target.name;
                  }
                 
              ]]>
          </fx:Script>
         
          <s:BorderContainer width="300" height="96" horizontalCenter="0" verticalCenter="0">
              <mx:Label text="Upload Your CV" fontWeight="bold" horizontalCenter="0" top="10"/>
              <mx:Label text="Choose a file..." id="statusText" horizontalCenter="0" top="35"/>
              <mx:Button click="uploadFile();" label="Upload File" horizontalCenter="0" bottom="10"/>
          </s:BorderContainer>
         
      </s:Application>

       

      While my PHP code it

      <?php

      $tempFile = $_FILES['Filedata']['tmp_name'];
      $fileName = $_FILES['Filedata']['name'];
      $fileSize = $_FILES['Filedata']['size'];
      $id = $_POST["id"]

      $newName = '$id'.'.doc';
      move_uploaded_file($tempFile, "./" . $newName);

      ?>

       

      What i am trying to do is, when the user clicks the "Upload File" button on the flex app, after they pick a file, it currently uploads that file using the name of the given file.  But what i want to do, is to pass the variable "ID" to the PHP and use that as the file name instead.

       

      Im a little bit stumped as to how to do this ?  I dont know where to include the code to pass "ID",  normally i deal with HTTPSservice's where you declare such things in the function.


      Any help would be great.!

        • 1. Re: Upload Component, Pass extra Variable
          Shongrunden Adobe Employee

          You should be able to add a parameter using URLVariables, so change your init function to something like:

           

                      private function init():void {
                          urlRequest = new URLRequest(serverSideScript);
                          var variables:URLVariables = new URLVariables();
                          variables.myFileName = "flexponential";
                          urlRequest.data = variables;
                          fileReferenceList = new FileReferenceList();
                          fileReferenceList.addEventListener(Event.SELECT, fileSelectedHandler);
                      }

           

          And then in your PHP file you should be able to access that data via the post variable.

           

          $myFileName = $_POST["myFileName"];