11 Replies Latest reply on Feb 13, 2011 3:14 PM by Sunilchitty

    Flex Save TextArea text directly to server that swf sits on

    djh88ukwb Level 1

      Hello all.

       

      I am trying to save the text that is contained within a TextArea directly to the server that my application is sitting on.

       

      I have been able to save it directly to the local machine using the following code

       

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

                  private var fileReference:FileReference = new FileReference();
                  private function onSaveText():void
                  {
                      fileReference.save(myText.text,"myText.txt");
                  }
                  private function onLoadText():void
                  {
                      var f:FileFilter = new FileFilter("Text","*.txt");
                      fileReference.browse([f]);
                      fileReference.addEventListener(Event.SELECT,onFileSelect);
                      fileReference.addEventListener(Event.COMPLETE,onFileComplete);
                  }
                  private function onFileSelect(event:Event):void
                  {
                      fileReference.load();
                  }
                  private function onFileComplete(event:Event):void
                  {
                      myText.text = fileReference.data.readUTFBytes(fileReference.data.length);
                  }
                 
              ]]>
          </mx:Script>
         
          <mx:Panel id="myPanel" width="400" height="450" verticalScrollPolicy="off"
                    horizontalScrollPolicy="off" title="Text Pad">
              <mx:TextArea width="380" height="420" id="myText"
                           text="Hello You Can Save This to test this APP"/>
              <mx:ControlBar>
                  <mx:Button label="Save Text" click="onSaveText()"/>
                  <mx:Button label="Load Text" click="onLoadText()"/>
              </mx:ControlBar>
          </mx:Panel> 
         
      </mx:Application>

       

      Is it possible to save directly to the server?

        • 1. Re: Flex Save TextArea text directly to server that swf sits on
          BhaskerChari Level 4

          Hi djh88ukwb,

           

          It is not directly possible to save the text file directly to the server as now the edited file resides on local hard disk...Once after you download the file and edit the file to make changes and if you want to update the text file to the server then you need to upload the text file to the server using the fileReference.upload().

           

           

          Thanks,

          Bhasker Chari

          • 2. Re: Flex Save TextArea text directly to server that swf sits on
            djh88ukwb Level 1

            Im sorry i think the example above was mis leading . . .

             

            What i am trying to create ultimatley is this.

             

            The user loads the app, and by carrying out the actions it populates an array, this array is then converted into XML format and displayed in a textarea, nothing has been saved or uploaded yet, the XML file is just in the textarea.

             

            What i want to do then, is when the user hits the "SAVE" button, a function is called that saves the contents of the textarea as a XML file on the server.

             

            The endpoint for the XML file and the location of the swf file is on the same server.

             

            Now i dont knoe if this is possible?

             

            Would i need some sort of PHP script ?

             

            Pass the textarea data to the script, and then create the file in the php script?

             

            Thanks

            • 3. Re: Flex Save TextArea text directly to server that swf sits on
              BhaskerChari Level 4

              Hi djh88ukwb,

               

              I think you can do either of the below things:

              1) You can write the xml contents of the TextArea to an xml file local on the disc and after that try to upload that xml file to the server and write the the logic on the PHP server to save the uploaded xml file.

               

              2) You can pass the data to the server side using the Post Method ans try to read the contents on the server side and generate the file also on server side and save it.

               

               

              Thanks,

              Bhasker Chari

              1 person found this helpful
              • 4. Re: Flex Save TextArea text directly to server that swf sits on
                Matt Le Fevre Level 4

                Would i need some sort of PHP script ?

                 

                Pass the textarea data to the script, and then create the file in the php script?

                 

                Thanks

                 

                yep, easiest way i can imagine.

                 

                 

                Store the textarea.text to a string variable in flex, pass it to a HTTPService, POST it to a php script

                 

                <?php
                
                
                
                $stringToWrite = "<txtArea>".$_POST["postedString"]."</txtArea>";
                
                
                
                $fh = fopen("yourFilName.xml", 'w');
                fwrite($fh, $stringToWrite );
                fclose($fh);
                
                ?>
                
                
                • 5. Re: Flex Save TextArea text directly to server that swf sits on
                  djh88ukwb Level 1

                  Hello, Thanks for this, however i am having a few problems =[

                   

                  The XML file is being created but nothing is going to it, here is my code

                   

                  Flex:

                          <mx:HTTPService id="createXML" url="newuser.php" method="POST" result="createXMLResult(event)">
                              <mx:request xmlns="">
                                  <id>{ID}</id>
                                  <text>{textareaString}</text>
                              </mx:request>
                          </mx:HTTPService>

                  When i click the save button this is the function that gets called

                              private function createXMLfunc():void
                              {
                                  textareaString = textArea2.text;
                                  vartxt.text = textareaString;
                                  createXML.send()
                              }

                  I added a second text area to see if the string contained all the correct information, and it does.

                   

                  PHP script

                  <?php

                  $loadedID  = mysql_real_escape_string($_POST["id"]);
                  $newtext  = mysql_real_escape_string($_POST["text"]);
                  $newloadedID = $loadedID + '.xml';

                  $filename= $newloadedID;
                  $mystring='text';
                  $stringToWrite='change';

                  if (($fh = @fopen($filename, "w")) !== false)
                  {
                  if (@fwrite($fh, $mystring) === false)
                  {
                  die(sprintf("Unable to write to file: %s!", $filename));
                  }
                  fclose($fh);
                  }
                  else
                  {
                  die(sprintf("Could not open file %s for writing!", $filename));
                  }

                  $file = file_get_contents($newloadedID);
                  $file = str_replace('text', $newtext, $file);
                  file_put_contents('45.xml', $file);

                  ?>

                   

                  At first the script makes a xml file, and puts "text" inside it,  it then should repalce this "text" which the contents of the string, however it dosent it just comes back blank.

                   

                  if i change it to this

                  $file = str_replace('text', 'changed', $file);

                   

                  The xml file contained "changed", so i know everything is working in terms of creating the file and putting data in it, its just not putting the string form the Flex in it,  any ideas?

                  • 6. Re: Flex Save TextArea text directly to server that swf sits on
                    djh88ukwb Level 1

                    Ok i have made some changes whilst trying to get to the root of the problem, but am still having problems.

                     

                    I have changed my php script to the following

                     

                     

                    <?php

                    $stringToWrite = "<text>".$_POST["textareaString"]."</text>";

                    $fh = fopen("test.xml", 'w');
                    fwrite($fh, $stringToWrite  );
                    fclose($fh);
                    ?>

                     

                    THe same as you recommended,  but when i open the test.xml file all i get is

                     

                    <text></text>

                     

                    Which leaves me to believe that the string is not being taken form flex, although i publish the string to a textarea before i call my httpservice . .

                     

                                private function createXMLfunc():void
                                {
                                    textareaString = textArea2.text;
                                    vartxt.text = textareaString;
                                    createXML.send()
                                }

                     

                    So i know the data is in the string, but its not sending :S

                     

                            <mx:HTTPService id="createXML" url="newuser.php" method="POST" result="createXMLResult(event)">
                                <mx:request xmlns="">
                                    <id>{ID}</id>
                                    <text>{textareaString}</text>
                                </mx:request>
                            </mx:HTTPService>

                     

                     

                    Any ideas?

                    • 7. Re: Flex Save TextArea text directly to server that swf sits on
                      Matt Le Fevre Level 4

                      djh88ukwb wrote:

                       

                      Ok i have made some changes whilst trying to get to the root of the problem, but am still having problems.

                       

                      I have changed my php script to the following

                       

                       

                      <?php

                      $stringToWrite = "<text>".$_POST["textareaString"]."</text>";

                      $fh = fopen("test.xml", 'w');
                      fwrite($fh, $stringToWrite  );
                      fclose($fh);
                      ?>

                       

                      THe same as you recommended,  but when i open the test.xml file all i get is

                       

                      <text></text>

                       

                      Which leaves me to believe that the string is not being taken form flex, although i publish the string to a textarea before i call my httpservice . .

                       

                                  private function createXMLfunc():void
                                  {
                                      textareaString = textArea2.text;
                                      vartxt.text = textareaString;
                                      createXML.send()
                                  }

                       

                      So i know the data is in the string, but its not sending :S

                       

                              <mx:HTTPService id="createXML" url="newuser.php" method="POST" result="createXMLResult(event)">
                                  <mx:request xmlns="">
                                      <id>{ID}</id>
                                      <text>{textareaString}</text>
                                  </mx:request>
                              </mx:HTTPService>

                       

                       

                      Any ideas?

                       

                      change your PHP file to this

                       

                      <?php

                      $stringToWrite = "<text>".$_POST["text"]."</text>";

                      $fileID = $_POST["id"];

                       

                      $newFileName = $fileID.'.xml';

                      $fh = fopen($newFileName, 'w');
                      fwrite($fh, $stringToWrite  );
                      fclose($fh);
                      ?>

                       

                      to coresspond to your own app, i only put

                       

                      $_POST["textareaString"]

                      as an example

                      1 person found this helpful
                      • 8. Re: Flex Save TextArea text directly to server that swf sits on
                        djh88ukwb Level 1

                         

                        Thank you,

                         

                        One last thing thou sorry.

                         

                        My flex file adds <question></question> to the xml file automatically, now in that php file its adding <text></text>,

                         

                        Is there a way to remove these?  I need to keep the <question>tags, i tried simply removing them from the php file but it all went pants.

                         

                        <text><question>
                          <item>
                            <mx_internal_uid>AD379966-8769-6A57-283B-D10F29F4208B</mx_internal_uid>
                            <question>What is your name</question>
                            <start>1.614</start>
                            <stop>3.611</stop>
                          </item>
                          <item>
                            <mx_internal_uid>6B75B192-5062-1557-C130-D10F48B15259</mx_internal_uid>
                            <question>How old are you ?</question>
                            <start>4.819</start>
                            <stop>6.258</stop>
                          </item>
                        </question></text>

                         

                        If you see i just need <question> and not <text>

                        • 9. Re: Flex Save TextArea text directly to server that swf sits on
                          djh88ukwb Level 1

                          No worries ! fixed it, just changed it to this

                           

                          <?php
                          $loadedID  = mysql_real_escape_string($_POST["id"]);

                          $stringToWrite = "".$_POST["text"]."";

                          $fh = fopen("test.xml", 'w');
                          fwrite($fh, $stringToWrite  );
                          fclose($fh);
                          ?>

                           

                          But one last question now, (honest last one).

                           

                          I am bringing an ID in with this code,

                           

                          Now i want the file name to be the name of the ID.  so for example,

                           

                          if ID = 4 filename should be 4.xml, instead of test,  

                           

                          How can i alter this to allow for this ?  What i try keeps getting to files saved, not just the one. =\

                          • 10. Re: Flex Save TextArea text directly to server that swf sits on
                            Matt Le Fevre Level 4

                            already expected you to ask this so i did it for you above

                             


                            $fileID = $_POST["id"];

                            $newFileName = $fileID.'.xml';

                            $fh = fopen($newFileName, 'w');
                            fwrite($fh, $stringToWrite  );
                            fclose($fh);

                             

                            have bolded the parts that do it for you

                            • 11. Re: Flex Save TextArea text directly to server that swf sits on
                              Sunilchitty

                              Hello Frnds,

                               

                              I need to implement the same concept using java. I am able to save the data from the text area into a text file, however, I need to save the file in the server.

                              Can anyone please help me out ?