2 Replies Latest reply on Jul 10, 2008 5:54 AM by nitsmooth

    sending data from php to flex

    nitsmooth
      How can i send data from a php page to the flex page..and the flex page in this case is not calling the flex page so i cannot use the return keyword
        • 1. Re: sending data from php to flex
          SujitG Level 2
          Hi,

          Please check out flashVars. Please find more details on how to pass data using flashVars.

          http://livedocs.adobe.com/flex/3/html/passingarguments_3.html

          Hope this helps.
          • 2. Re: sending data from php to flex
            nitsmooth Level 1
            man if u could tell me exactly wt to change in my code

            sample1.mxml : this file redirects the image browsed from my local system to the php page upload.php whose details u will see below


            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init();">
            <mx:Script>
            <![CDATA[

            private var urlRequest:URLRequest;
            private var fileReferenceList:FileReferenceList;
            private var serverSideScript:String = " http://localhost/upload.php";

            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;
            }

            ]]>
            </mx:Script>

            <mx:Label text="Upload File From Flex to PHP" fontWeight="bold"/>
            <mx:Label text="Choose a file..." id="statusText"/>
            <mx:Button click="uploadFile();" label="Upload File"/>

            </mx:Application>


            ------------------------------------------------------------------------------------------ ---------------
            upload.php : uploads the image to the localhost directory


            <?php

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

            $fp = fopen("test.txt","a+");
            fwrite($fp,$fileName."\r\n");

            move_uploaded_file($tempFile, "./" . $fileName);
            //header("Location : C:\Documents and Settings\Manohar\My Documents\Flex Builder 3\sample\src\dload.mxml" );
            ?>
            ------------------------------------------------------------------------------------------ ------

            Now i want to send this image $fileName from the upload.php to the flex page dload.mxml so that i can insert this image in a textarea which is being done by dload.mxml .But how do i send the $fileName to the dload.mxml from upload.php

            and if i get the $fileName transferred to the dload.mxml page what path to give in imgsrc=


            plz help!!!
            ------------------------------------------------------------------------------------------ --------------
            dload.mxml


            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="this.onApplicationComplete()">

            <mx:Script>
            <![CDATA[

            import flash.display.Sprite;
            import flash.events.*;
            import flash.net.FileFilter;
            import flash.net.FileReference;
            import flash.net.URLRequest;


            private var file:FileReference;


            private function onApplicationComplete():void
            {
            file = new FileReference();
            configureListeners(file);
            }

            private function configureListeners(dispatcher:IEventDispatcher):void
            {
            dispatcher.addEventListener(Event.SELECT, selectHandler);
            }

            private function selectHandler(event:Event):void
            {
            var file:FileReference = FileReference(event.target);
            outputTextArea.htmlText = "<img src='http:\\localhost\\" + (file.name) + "'/>";
            trace(outputTextArea.htmlText);
            }
            ]]>
            </mx:Script>

            <mx:Button
            label="Browse"
            horizontalCenter="-24"
            bottom="91"
            click="file.browse();">
            </mx:Button>

            <mx:TextArea id="outputTextArea"
            width="793"
            height="583" x="0" y="0" >
            </mx:TextArea>

            </mx:Application>